Android : restart application after update - ACTION_PACKAGE_REPLACED

后端 未结 5 1432
情歌与酒
情歌与酒 2020-12-03 10:09

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 applicati

相关标签:
5条回答
  • 2020-12-03 11:03

    After hours and hours of searching how to restart your app after it was updated, I finally find why it won't restart.

    The app must NOT be launched by Android Studio or other IDE. If I manually install app with its apk and launch it from the current Android device, the app can recognize if there was an update of my application and it restarts correctly.

    My user case is a custom launcher that can update by itself launching a PackageInstaller.Session without going to Google Play Store

    Here it's the code

    Manifest:

    <receiver android:name="UpdateReceiver" >
         <intent-filter>
             <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
         </intent-filter>
    </receiver>
    

    UpdateReceiver (Kotlin code):

    class UpdateReceiver: BroadcastReceiver() {
    
        companion object {
            private val TAG = UpdateReceiver::class.java.simpleName
        }
    
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.d(TAG, "onReceive ${intent?.action ?: "unknown action"}")
            if (intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
              
                //MainActivity = Your first activity
                val yourIntent = Intent(context, MainActivity::class.java)
                yourIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context?.startActivity(yourIntent)
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-03 11:04

    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

    0 讨论(0)
  • 2020-12-03 11:06

    For anyone trying all the other answers without success, I recommend you restarting the app.

    I noticed the app was successfully updated, but it wasn't being launched even though I tried starting the activity in many ways.

    I used https://github.com/JakeWharton/ProcessPhoenix in the BroadcastReceiver, so basically in the update you're downloading, add ProcessPhoenix.triggerRebirth(context); in your BroadcastReceiver, or restart the app somehow in there.

    0 讨论(0)
  • 2020-12-03 11:07

    After two days of struggle and experimentation, I found out the reason. It turned out I need to carefully read the official documentation.

    As said here: https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED

    It turned out the key phrase is: It does not contain any additional data

    those. if you changed the manifest, then the application will not restart after updating

    you are welcome

    0 讨论(0)
  • 2020-12-03 11:09

    I put the following receiver in the AndroidManifest.xml

    <receiver android:name=".StartupReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
        </intent-filter>
    </receiver>
    

    So my app can be launched on update as well as device reboot. Ofcourse as everyone has mentioned that you need API 12+ for MY_PACKAGE_REPLACED.

    0 讨论(0)
提交回复
热议问题