Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

broadcast - Android : restart application after update - ACTION_PACKAGE_REPLACED

My application that is not on Play Store verify on the web If there are a new version and download and start it. After the installation I would like to restart the application and I would use a BroadcastRecevier with ACTION_PACKAGE_REPLACED. This is the code :

Broadcast:

public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
    ApplicationInfo app = new ApplicationInfo();
    if(app.packageName.equals("it.android.downloadapk")){
      Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
      context.startActivity(LaunchIntent);                    
    }
  }
}

Manifest:

<receiver android:name="it.android.downloadapk.Broadcast">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
    <data android:scheme="package" android:path="it.android.downloadapk" /> 
  </intent-filter>
</receiver>

The problem is that when I install new apk, the Broadcast appears not to start, why ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

see this:

How to know my Android application has been upgraded in order to reset an alarm?

correct fix is that you use the wrong string in the manifest: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED

it should be "android.intent.action.PACKAGE_REPLACED" instead.


ok , i see that what i've written is still not enough to try it out, so i will make an exception and publish a whole project just to show that it works: app code is in a package called "com.broadcast_receiver_test" . don't forget to run it before testing , or else it won't work on some android versions (i think API 11+) .

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.broadcast_receiver_test" android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="3" />

  <application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">

    <activity android:name=".BroadcastReceiverTestActivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <receiver android:name=".MyBroadcastReceiver">
      <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"/>
        <data android:scheme="package"  />
      </intent-filter>

      <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED"/>
        <data android:scheme="package"  />
      </intent-filter>

      <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED"/>
        <data android:scheme="package"  />
      </intent-filter>
    </receiver>

  </application>
</manifest>

MyBroadcastReceiver.java:

public class MyBroadcastReceiver extends BroadcastReceiver
  {
  @Override
  public void onReceive(final Context context,final Intent intent)
    {
    final String msg="intent:"+intent+" action:"+intent.getAction();
    Log.d("DEBUG",msg);
    Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
  }

please just run it and see that it works perfectly .


EDIT: if your app is for API12 and above, and only wish to handle the case of updating of your app, you can use this intent alone:

http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...