目前市面上很多工具有屏蔽其他app开机启动的功能,比如猎豹清理大师和360安全卫士。本文讲解如何避免被猎豹清理大师关闭收不到开机广播的情况,360安全卫士的屏蔽模式还没搞清楚,有大牛明白的话请赐教。
猎豹屏蔽的原理是通过改变第三方app接收开机广播reciver的状态来达到效果。解决办法就是在关机广播里面把自己改为可用。
具体代码如下:
接收开机广播android.intent.action.BOOT_COMPLETED的组建命名为com.android.test.CompletedReciver.java
接收关机广播android.intent.action.ACTION_SHUTDOWN的组建命名为com.android.test.ShutDownReciver.java
package com.android.test;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
public class ShutDownReceiverextends BroadcastReceiver {
private static
void setComponentEnableSetting(Context context, String className) {
PackageManager pm = context.getPackageManager();
ComponentName componentName = new ComponentName(context, className);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private static
int getComponentEnableSetting(Context context, String className) {
PackageManager pm = context.getPackageManager();
ComponentName componentName = new ComponentName(context, className);
return pm.getComponentEnabledSetting(componentName);
}
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (TextUtils.isEmpty(action)) {
return;
}
if (Intent.ACTION_SHUTDOWN.equals(action)) {
String ComponentName = "com.android.test.CompletedReciver";
if (PackageManager.COMPONENT_ENABLED_STATE_DISABLED == getComponentEnableSetting(context.getApplicationContext(), ComponentName)) {
setComponentEnableSetting(context.getApplicationContext(), ComponentName);
}
}
}
}
关于360安全卫士和MIUI系统自带安全中心的实现方式还没搞明白。