Android Broadcastreceiver for other apps install/delete not working

无人久伴 提交于 2019-12-24 10:09:11

问题


I have a Broadcastreceiver to detect other apps installs or deletion.

This is my Java

public class AppListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context var1, Intent var2) {
        // TODO Auto-generated method stub

        Log.d("AppTag", "Received!");
}
}

This is my manifest

<receiver android:name=".AppListener">
        <intent-filter android:priority="999">
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

But whenever I install or delete an app nothing occurs!


回答1:


You are trying to listen to broadcasts like ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REPLACED. That is fine for Android 7.1 and lower. On Android 8.0+, you cannot register for those broadcasts in the manifest, as most implicit broadcasts are banned.

Instead, you need to call getChangedPackages() on PackageManager periodically, such as via WorkManager. This will not give you real-time results, but real-time results are no longer an option on Android 8.0+.



来源:https://stackoverflow.com/questions/57537002/android-broadcastreceiver-for-other-apps-install-delete-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!