After the PackageInstaller successfully (self) updates the application, the application closes and doesn't launch again.
Possible duplicate: Android PackageInstaller, re-open the app after it updates itself
The situation is pretty similar, ill show some code snippets first.
AndroidManifest.xml
<receiver android:name=".UpdateReceiver" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
My PackageInstaller session runs in an AsyncTask:
PackageInstaller packageInstaller = ctx.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams parameters = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
parameters.setAppPackageName(StaticData.TerminalPackageName);
progressChanged(10);
try {
int sessionId = packageInstaller.createSession(parameters);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
ProgressApproximatorHandler progress = startProgressApproximator(10, 100, 5);
OutputStream installerStream = session.openWrite("INSTALL_TERMINAL", 0, mApplicationLength);
installerStream.write(firmwareData, 0, mApplicationLength);
// This call ensures every byte on the stream is committed to disk.
session.fsync(installerStream);
installerStream.close();
Intent intent = new Intent(ctx, UpdateReceiver.class);
intent.setAction(Intents.APK_INSTALL_FINISHED_INTENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
mContext.get(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Sealing the session.
session.commit(pendingIntent.getIntentSender());
session.close();
progress.stop();
} catch (IOException e) {
Logger.error(mLogTag, e, "Failed to create an installer session.");
return false;
}
At the end of this snippet, you can see the setup of an Intent to send. I've tried many variations and none of them worked. For example:
Intent intent = new Intent(Intents.APK_INSTALL_FINISHED_INTENT);
and
Intent intent = new Intent("android.intent.action.MY_PACKAGE_REPLACED");
My receiver never receives the intent.
public class UpdateReceiver extends BroadcastReceiver {
private static final String TAG = "UPDATE";
@Override
public void onReceive(Context context, Intent intent) {
Logger.info(TAG, "Intent action: " + intent.getAction());
Logger.info(TAG, "updated");
}
}
EDIT: Somehow I managed to restart the app autimatically but it gets killed after a 1-3 seconds. I found the following error in the logcat of the system.
W/ActivityManager: Unable to send startActivity intent
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MY_PACKAGE_REPLACED from pid=-1, uid=10071
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18195)
at com.android.server.am.ActivityManagerService.broadcastIntentInPackage(ActivityManagerService.java:18777)
at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:308)
at com.android.server.am.PendingIntentRecord.sendWithResult(PendingIntentRecord.java:205)
at com.android.server.am.ActivityManagerService.sendIntentSender(ActivityManagerService.java:7409)
at android.content.IntentSender.sendIntent(IntentSender.java:190)
at android.content.IntentSender.sendIntent(IntentSender.java:154)
at com.android.server.pm.PackageInstallerService$PackageInstallObserverAdapter.onPackageInstalled(PackageInstallerService.java:1069)
at android.app.PackageInstallObserver$1.onPackageInstalled(PackageInstallObserver.java:34)
at com.android.server.pm.PackageInstallerSession.dispatchSessionFinished(PackageInstallerSession.java:1137)
at com.android.server.pm.PackageInstallerSession.-wrap2(PackageInstallerSession.java)
at com.android.server.pm.PackageInstallerSession$4.onPackageInstalled(PackageInstallerSession.java:648)
at com.android.server.pm.PackageManagerService.handlePackagePostInstall(PackageManagerService.java:1809)
at com.android.server.pm.PackageManagerService.-wrap25(PackageManagerService.java)
at com.android.server.pm.PackageManagerService$PackageHandler.doHandleMessage(PackageManagerService.java:1438)
at com.android.server.pm.PackageManagerService$PackageHandler.handleMessage(PackageManagerService.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
I am not sure if this is the reason for it.
EDIT2: Now I receive the intent, thus the app is running. The problem now is that its in the back stack.
I have a launcher app that could re-launch the main (updated) app but that doesn't sound clean code to me.
EDIT3:
I tried David's answer but I receive the following error:
E/ActivityManager: Failure starting process com.example.myapp
java.lang.SecurityException: Package com.example.myapp is currently frozen!
at com.android.server.pm.PackageManagerService.checkPackageStartable(PackageManagerService.java:3197)
at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3789)
at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3749)
at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3630)
at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:1255)
at com.android.server.am.BroadcastQueue$BroadcastHandler.handleMessage(BroadcastQueue.java:172)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Note this is thrown by the android system, and not by my app.
The MY_PACKAGE_REPLACED
Intent
is broadcast by Android after your app is updated.
To restart your app after the update, you could pass a launch Intent
to the PackageInstaller
, like this:
Intent intent = getPackageManager().getLaunchIntentForPackage("my.package.name");
PendingIntent pendingIntent = PendingIntent.getActivity(
mContext.get(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
This should cause your app to be restarted after the update is completed.