Android8.0安装/卸载广播的注册收不到

Android8.0安装/卸载广播的注册收不到

以下是广播的代码

public class AppInstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PackageManager manager = context.getPackageManager();
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "安装成功"+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "卸载成功"+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "替换成功"+packageName, Toast.LENGTH_LONG).show();
        }
        

    }

}

以上代码大家都知道,不多解释
以下是注册代码

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 		  
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
context.registerReceiver(this, intentFilter);

咋一看没什么问题,但就是收不到,别着急,把这句加上

intentFilter.addDataScheme("package");

然后就收到了

最终的代码

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 		  
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
intentFilter.addDataScheme("package");
context.registerReceiver(this, intentFilter);

你可能感兴趣的:(Android)