其他应用程序的Android Broadcastreceiver安装/删除无法正常工作 [英] Android Broadcastreceiver for other apps install/delete not working
问题描述
我有一个Broadcastreceiver,可以检测其他应用的安装或删除.
I have a Broadcastreceiver to detect other apps installs or deletion.
这是我的Java
public class AppListener extends BroadcastReceiver {
@Override
public void onReceive(Context var1, Intent var2) {
// TODO Auto-generated method stub
Log.d("AppTag", "Received!");
}
}
这是我的清单
<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!
推荐答案
您正在尝试收听 ACTION_PACKAGE_ADDED
和 ACTION_PACKAGE_REPLACED
之类的广播.这对于Android 7.1及更低版本来说很好.在Android 8.0+上,您不能在清单中注册这些广播,如大多数隐式广播均被禁止.
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.
相反,您需要调用 PackageManager
上> getChangedPackages(),例如通过 WorkManager
.这不会为您提供实时结果,但是在Android 8.0+上不再可以选择实时结果.
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+.
这篇关于其他应用程序的Android Broadcastreceiver安装/删除无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!