3

I'm developing a non-public Android app, i.e. the app won't be available in the global Android Market. The app will be installed on a limited number of clients, e.g. by using an apk file.I Have an .apk in SD Card and I am trying to update my application from my application. For that, I'm Using Intent

My Code

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/" +"Test.apk")), "application/vnd.android.package-archive");
startActivity(intent);

NOTE : It is working fine, but after updating it, application will be closed.

The question is "How to Prevent This ?"

I'm also use Broadcast Receiver For Re-Open my app

public class AutoStart extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Intent i = new Intent(context, ABCActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
    }else{
        
         Intent i = new Intent(context, XYZActivity.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(i);
    }
}

Problem 1 :- Can't Re-Open Activity When

"android.intent.action.PACKAGE_ADDED",

"android.intent.action.PACKAGE_INSTALL",

"android.intent.action.PACKAGE_CHANGED"

<receiver
        android:name=".AutoStart"
        android:enabled="true"
        android:exported="true" >
       
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package" />
        </intent-filter>
        
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <data android:scheme="package" />
        </intent-filter>
        
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <data android:scheme="package" />
        </intent-filter>
        
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
        
    </receiver>

"android.intent.action.BOOT_COMPLETED" Working Properly

Permissions Given

1 >     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2 >     <uses-permission android:name="android.permission.RESTART_PACKAGES" />
3 >     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Community
  • 1
  • 1
Karan Mavadhiya
  • 1,042
  • 8
  • 23
  • 2
    Whenever you install an apk, if a previous version of the app exists, it will be fully stopped *specifically so it can be upgraded*. I haven't tried this myself, but maybe [this](http://stackoverflow.com/questions/15572154/auto-restart-app-after-market-update) can be of help to restart your app. – A--C Aug 10 '13 at 05:51
  • I am Trying This also.. http://stackoverflow.com/questions/15572154/auto-restart-app-after-market-update – Karan Mavadhiya Aug 10 '13 at 07:22
  • That is the exact same question I linked to in my comment. – A--C Aug 10 '13 at 15:46
  • Yes But Its Not Working.... Any Other Suggestion?? @A--C – Karan Mavadhiya Aug 12 '13 at 07:15

3 Answers3

7

unfortunately you can't prevent process from been terminated when it re-install itself, and from the other hand - you'll never receive any broadcast if it your own app that been re-install at that time.

if I had to implement such feature, I would solve the problem like this:

trick number 1:

creating another app (that would be installed somehow also on your user's device) that it only roll is to act like sort of a "watch-dog": this app would listen to installation broadcasts, and because it's different app then the one who been installed - it won't have problem to launch to "original app".

of-course don't forget that the user will need to "launch" at least ones this watch-dog app, because from android 4 receivers won't work until the app process started at least ones from. that is for security reasons..

trick number 2:

another option would be to registered in manifest you app also to ACTION_TIME_TICK broadcast. you can count on this broadcast to be called each number of seconds, and implement when it receives some logic that recognize if the app right now need to launch the main activity or not.

this approach will work, but from performances reason would be not that good - because you app process would be alive almost all the time because it reacts to that broadcast. if you don't care about that - so it not problem

trick number 3:

provide pendingIntent to alarmManager just before you start the installation activity to a 30-50 seconds after. the pending intent will hold intent to re-launch your app.

assuming the app would be installed until then - it would work.

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
2

The accepted answer is incorrect.

You can simply add FLAG_ACTIVITY_NEW_TASK to the intent, that starts the installer. This way the installer will run on a separate process and it won't be killed after replacing the package with the new version.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/" +"Test.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
tbm
  • 21
  • 2
1

Try using android.intent.action.MY_PACKAGE_REPLACED

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

yoah
  • 7,180
  • 2
  • 30
  • 30