Sac*_*ngh 4 java android intentfilter broadcastreceiver
我已经创建了一个广播接收器并注册了它,但我无法接收 Intent.ACTION_PACKAGE_ADDED 事件。
val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
}
intentFilter.priority = 999
val rec = IntentReceiver()
registerReceiver(rec, intentFilter)
Run Code Online (Sandbox Code Playgroud)
这是我的 BroadcastReceiver 类
class IntentReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(context, "TEST TOAST", Toast.LENGTH_LONG).show()
val builder =
NotificationCompat.Builder(context!!, "worker_channel")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("App is installed")
.setPriority(NotificationCompat.PRIORITY_HIGH)
val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(4, builder.build())
}}
Run Code Online (Sandbox Code Playgroud)
我没有收到任何事件,但是相同的广播接收器适用于以下意图:
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_AIRPLANE_MODE_CHANGED
Run Code Online (Sandbox Code Playgroud)
而且我还在 Logcat 中获取 PACKAGE_ADDED 事件的日志,如下所示
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.socialnmobile.dictapps.notepad.color.note flg=0x4000010 (has extras) } to com.google.android.gms/.gass.chimera.PackageChangeBroadcastReceiver
Run Code Online (Sandbox Code Playgroud)
我已经在 android 11 和 12 上进行了测试,除了 Intent.ACTION_PACKAGE_ADDED 之外,其他所有意图都工作正常,我是否遗漏了任何内容或做错了什么?
Sac*_*ngh 10
问题已解决,我必须在 10 之后添加查询 Android 版本的所有软件包权限,不知道为什么文档中没有提到它。
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES" />
Run Code Online (Sandbox Code Playgroud)