My use case is that when a new version of the application is available, I display the update information. However, after the user updates the app, it closes and does not restart automatically. The user has to manually navigate to the app and click the icon to launch it.
I used Broadcast Receiver using the "MY_PACKAGE_REPLACED" in the manifest but it is not working.
And i am getting the error as
*BroadcastQueue: Background execution not allowed:
In manifest.xml
<receiver
android:name=".AppInstallationReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Receiver
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
Log.e(TAG, "Package replaced, starting the service");
Intent serviceIntent = new Intent(context, InstallationCheckerService.class);
ContextCompat.startForegroundService(context, serviceIntent);
}
}
Error:
Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.application.dev.android flg=0x4000010 (has extras) } to com.android.packageinstaller/.PackageInstalledReceiver
My use case is that when a new version of the application is available, I display the update information. However, after the user updates the app, it closes and does not restart automatically. The user has to manually navigate to the app and click the icon to launch it.
I used Broadcast Receiver using the "MY_PACKAGE_REPLACED" in the manifest but it is not working.
And i am getting the error as
*BroadcastQueue: Background execution not allowed:
In manifest.xml
<receiver
android:name=".AppInstallationReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Receiver
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
Log.e(TAG, "Package replaced, starting the service");
Intent serviceIntent = new Intent(context, InstallationCheckerService.class);
ContextCompat.startForegroundService(context, serviceIntent);
}
}
Error:
Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.application.dev.android flg=0x4000010 (has extras) } to com.android.packageinstaller/.PackageInstalledReceiver
Share
Improve this question
edited Feb 4 at 8:11
dev_monster
asked Feb 4 at 5:24
dev_monsterdev_monster
12 bronze badges
1
- Is there any other way to start the application after the install is also ok – dev_monster Commented Feb 4 at 7:07
2 Answers
Reset to default 1Add the lines mentioned below in your AndroidManifest.xml
file:
<receiver android:name=".UpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
Create the broadcast receiver class and in the onReceive()
method add the lines below:
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) {
Intent restartIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (restartIntent != null) {
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(restartIntent);
}
}
}
Try this, it should work.
- Try explicit broadcast (MY_PACKAGE_REPLACED)
- If that fails, use a foreground service
- If you control the update flow, use a delayed restart via PendingIntent