title: Launch application after PackageInstaller finished (self) updating
body: <p>After the PackageInstaller successfully (self) updates the application, the application closes and doesn't launch again.</p>
  <p>Possible duplicate: <a href="https://stackoverflow.com/questions/43940990/android-packageinstaller-re-open-the-app-after-it-updates-itself">Android PackageInstaller, re-open the app after it updates itself</a></p>
  <p>The situation is pretty similar, ill show some code snippets first.</p>
  <p>AndroidManifest.xml</p>
  <pre><code>&lt;receiver android:name=&quot;.UpdateReceiver&quot; &gt;
      &lt;intent-filter&gt;
          &lt;action android:name=&quot;android.intent.action.MY_PACKAGE_REPLACED&quot; /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  <p>My PackageInstaller session runs in an AsyncTask:</p>
  <pre><code>
  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(&quot;INSTALL_TERMINAL&quot;, 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, &quot;Failed to create an installer session.&quot;);
              return false;
          }
  </code></pre>
  <p>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:</p>
  <pre><code>Intent intent = new Intent(Intents.APK_INSTALL_FINISHED_INTENT);
  </code></pre>
  <p>and</p>
  <pre><code>Intent intent = new Intent(&quot;android.intent.action.MY_PACKAGE_REPLACED&quot;);
  </code></pre>
  <p>My receiver never receives the intent.</p>
  <pre><code>public class UpdateReceiver extends BroadcastReceiver {
      private static final String TAG = &quot;UPDATE&quot;;
      @Override
      public void onReceive(Context context, Intent intent) {
          Logger.info(TAG, &quot;Intent action: &quot; + intent.getAction());
          Logger.info(TAG, &quot;updated&quot;);
      }
  }
  </code></pre>
  <p>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.</p>
  <pre><code>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)
  </code></pre>
  <p>I am not sure if this is the reason for it.</p>
  <p>EDIT2: Now I receive the intent, thus the app is running. The problem now is that its in the back stack.</p>
  <p>I have a launcher app that <strong>could</strong> re-launch the main (updated) app but that doesn't sound clean code to me.</p>
  <p>EDIT3:</p>
  <p>I tried David's answer but I receive the following error:</p>
  <pre><code>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)
  </code></pre>
  <p>Note this is thrown by the android system, and not by my app.</p>