android API 26及以上有没有办法接收隐式广播
Is there any way to receive implicit broadcasts in android API 26 and above
如果目标设备上的应用程序更新或卸载,我想接收 BroadCast。根据文档,我可以使用 ACTION_PACKAGE_REPLACED and ACTION_PACKAGE_REMOVED.
Replace action: A new version of an application package has been installed, replacing an existing version that was previously installed.
Remove Action: An existing application package has been removed from the device. The data contains the name of the package. The package that is being removed does not receive this Intent.
所以我在前台服务中实现了这个:
val mIntentFilter = IntentFilter()
mIntentFilter.addAction("android.intent.action.PACKAGE_REMOVED")
mIntentFilter.addAction("android.intent.action.PACKAGE_REPLACED")
mIntentFilter.addDataScheme("package")
this.registerReceiver(applicationReceiver, mIntentFilter)
它非常适合删除操作,但不幸的是,我没有收到任何更新操作的广播。
我发现这是因为 Google 对 API 26 及更高版本施加的限制:
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
由于这是我的应用程序的主要功能之一,有人可以帮我处理这个问题吗?
你可以试试这个
class Test(var context: Context) {
var version: Long = -1
var pm: PackageManager = context.packageManager
var e: PackageManager.NameNotFoundException? = null
init {
val info = pm.getPackageInfo("com.google.maps", 0)
version = if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
info.longVersionCode
} else info.versionCode.toLong()
}
init {
e!!.printStackTrace()
}
}
然后将检索到的值与存储的值进行比较
如果目标设备上的应用程序更新或卸载,我想接收 BroadCast。根据文档,我可以使用 ACTION_PACKAGE_REPLACED and ACTION_PACKAGE_REMOVED.
Replace action: A new version of an application package has been installed, replacing an existing version that was previously installed.
Remove Action: An existing application package has been removed from the device. The data contains the name of the package. The package that is being removed does not receive this Intent.
所以我在前台服务中实现了这个:
val mIntentFilter = IntentFilter()
mIntentFilter.addAction("android.intent.action.PACKAGE_REMOVED")
mIntentFilter.addAction("android.intent.action.PACKAGE_REPLACED")
mIntentFilter.addDataScheme("package")
this.registerReceiver(applicationReceiver, mIntentFilter)
它非常适合删除操作,但不幸的是,我没有收到任何更新操作的广播。
我发现这是因为 Google 对 API 26 及更高版本施加的限制:
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
由于这是我的应用程序的主要功能之一,有人可以帮我处理这个问题吗?
你可以试试这个
class Test(var context: Context) {
var version: Long = -1
var pm: PackageManager = context.packageManager
var e: PackageManager.NameNotFoundException? = null
init {
val info = pm.getPackageInfo("com.google.maps", 0)
version = if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
info.longVersionCode
} else info.versionCode.toLong()
}
init {
e!!.printStackTrace()
}
}
然后将检索到的值与存储的值进行比较