Background execution not allowed receiving intent action package added - Packageinstalledreceiver

136 Views Asked by At

I developing an app updater (Kotlin) for my own app with PackageInstaller (android.content.pm.PackageInstaller) and without the Google Play. All of my code is in the one app and when the installation is done, the app is closed, but I don't get any success message by PackageInstaller. Checking the Logcat, I found this warning:

Background execution not allowed: receiving intent { act=android.intent.action.package_added dat="MY PACKAGE" flg=0x4000010 (has extras) } to com.google.android.packageinstaller/com.android.packageinstaller.packageinstalledreceiver

The PackageInstalledReceiver is the responsible to show the success message and restart the app. If I open my app, it is updated correctly, so my update code is working, but I need that the restart app.

The Android API version is 29.

CODE

The code below is the responsible to create the session and commited:

fun sessionCommit() {
    var session: Session? = null
    val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
    val sessionId = installer.createSession(params)
    session = installer.openSession(sessionId)
    addApkToInstallSession(
        session,
        packageName,
        File(this.context.filesDir, this.apkName)
    )
    session.commit(PendingIntent.getBroadcast(
        this.context,
        PENDING_INTENT_INSTALL,
        Intent(this.context, AppUpdaterReceiver::class.java),
        PendingIntent.FLAG_UPDATE_CURRENT
    ).intentSender);
}

And this is my broadcast code that work, because I receive the PackageInstaller.STATUS_PENDING_USER_ACTION correctly.

class AppUpdaterReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent != null) {
            val extras = intent.extras;
            if (extras != null) {
                val status = extras.getInt(PackageInstaller.EXTRA_STATUS);
                val message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
                when (status) {
                    PackageInstaller.STATUS_PENDING_USER_ACTION -> {
                        val confirmIntent = extras.get(Intent.EXTRA_INTENT) as Intent;
                        context.startActivity(confirmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                    }
                    PackageInstaller.STATUS_SUCCESS -> {
                        Toast.makeText(
                            context,
                            "Atualização concluída com sucesso!",
                            Toast.LENGTH_SHORT
                        ).show();
                    }
                    PackageInstaller.STATUS_FAILURE,
                    PackageInstaller.STATUS_FAILURE_ABORTED,
                    PackageInstaller.STATUS_FAILURE_BLOCKED,
                    PackageInstaller.STATUS_FAILURE_CONFLICT,
                    PackageInstaller.STATUS_FAILURE_INCOMPATIBLE,
                    PackageInstaller.STATUS_FAILURE_INVALID,
                    PackageInstaller.STATUS_FAILURE_STORAGE -> {
                        Toast.makeText(
                            context,
                            "Atualização falhou! $status, $message",
                            Toast.LENGTH_LONG
                        ).show();
                    }
                    else -> {
                        Toast.makeText(
                            context,
                            "A atualização retornou um status desconhecido: $status",
                            Toast.LENGTH_LONG
                        ).show();
                    }
                }
            }
        }
    }
}

I googled about this situation many, many times, but I can't set the allow the PackageInstalledReceiver receive the action about the app install.

I need that the restart app.

Sorry with I forget some information or put something of my code in another language, but it's my firts topic in English.

0

There are 0 best solutions below