Android 8.0去掉了部分隐式广播,例如网络的变化、app的安装和卸载等。这些广播使用之前的的静态注册已经不能接收到广播,需要动态注册才能收到广播。
APP安装的广播注册流程
private void registerInstallAppBroadcastReceiver() {
IntentFilter intentFilter =new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
StarNewsSdk.getContext().registerReceiver(mInstallAppBroadcastReceiver, intentFilter);
}
private BroadcastReceivermInstallAppBroadcastReceiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent !=null && TextUtils.equals(Intent.ACTION_PACKAGE_ADDED, intent.getAction())) {
if (intent.getData() !=null) {
String packageName = intent.getData().getSchemeSpecificPart();
Log.d(TAG,"安装的app的包名是-------->" + packageName);
}
}
}
}
};
网友评论