Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : restart application after update - ACTION_PACKAGE_REPLACED

Tags:

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 ?

like image 787
MisterX_Dev Avatar asked May 23 '12 21:05

MisterX_Dev


People also ask

How do I automatically restart apps on Android?

In order to restart your application when it crashed you should do the following : In the onCreate method, in your main activity initialize a PendingIntent member: Intent intent = PendingIntent. getActivity( YourApplication.

How do I restart an app in xamarin?

You can't explicitly restart an App - iOS specifically prohibits this, and there is no universal mechanism to do this on other platforms. If you determine that the session has expired, you will need to prompt the user to login, and do any initialization manually.


1 Answers

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

like image 115
android developer Avatar answered Oct 21 '22 01:10

android developer