Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

Android: package-related intents not fired

I'm developing a launcher application, I just added a broadcast receiver I will use to update the app list. I initially tried to receive ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REMOVED as they seemed enought for the job, however none where fired so I added all other package-related actions I deemed useful and tried to register for them both in the activity:

val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
        intentFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED)
        intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL)
        intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED)
        val rec = AppsInstallationsReceiver()
        registerReceiver(rec, intentFilter)

and manifest:

<receiver android:name=".core.installed_apps.AppsInstallationsReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>

The receiver itself is nothing special:

class AppsInstallationsReceiver : BroadcastReceiver() {

    override fun onReceive(p0: Context?, p1: Intent?) {
        //TODO
    }
}

turns out I only receive PACKAGE_FULLY_REMOVED and only if it's registered in the manifest. Now this is good enought, but I need a way to know when new apps are installed. Since this is a launcher app this feature is critical. Why am I not receiving anything? the activity of course still exists the background since it is used as launcher.

question from:https://stackoverflow.com/questions/65952773/android-package-related-intents-not-fired

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, you should be aware about the Implicit Broadcast Limitations introduced from Android 8.0 and above.

The broadcasts you registered come under the catergory of restricted broadcasts (except ACTION_PACKAGE_FULLY_REMOVED). So, you won't receive these events when you register the actions from manifest. So, remove the receiver entry from manifest.

The alternative solution to this would be to register these broadcasts programatically - which you have already done.

The only thing missing here is to add intentFilter.addDataScheme("package") before registering the receiver.

Hoping this should resolve your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...