最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - issue on Automatically starting the android application after the update - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 1

Add 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.

  1. Try explicit broadcast (MY_PACKAGE_REPLACED)
  2. If that fails, use a foreground service
  3. If you control the update flow, use a delayed restart via PendingIntent
发布评论

评论列表(0)

  1. 暂无评论