最近有个奇怪的想法,如何防止应用不小心被卸载呢?
简单的解决方案:将应用设置为设备管理器
public class Policy {
private DevicePolicyManager policyManager;
private ComponentName componentName;
public Policy(Activity activity) {
policyManager = (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(activity, DeviceAdminReceiver.class);
if (policyManager.isAdminActive(componentName)) {
return;
}
Intent intent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
intent.putExtra("android.app.extra.DEVICE_ADMIN", componentName);
activity.startActivity(intent);
}
void test() {
new Policy(null);
}
}
调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Policy(this);
}
配置AndroidManifest.xml
<receiver
android:name="android.app.admin.DeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
添加一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
</uses-policies>
</device-admin>