我想构建类似家长控制的应用,所以当孩子尝试卸载/删除我的应用时,我想要求用户在允许卸载/删除我的应用之前键入密码。
我试试这个,但仍然不明白:
Require a password to uninstall/remove application
有什么建议吗?
答案 0 :(得分:4)
如果使用设备管理,则可以锁定设备。用户无法卸载活动设备管理员,然后您可以在设备尝试禁用设备管理员时锁定设备,然后父母可以输入密码来解锁设备。
在你的清单中:
<receiver android:name=".AdminReceiver"
android:label="Administration"
android:description="@string/descript"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/deviceadmin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
然后在@xml/deviceadmin
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<reset-password />
<force-lock />
</uses-policies>
</device-admin>
然后
public class AdminReceiver extends DeviceAdminReceiver {
@Override
public CharSequence onDisableRequested(final Context context, Intent intent) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain); //switch to the home screen, not totally necessary
lockPhone(context, secPassword);
//Log.i(TAG, "DEVICE ADMINISTRATION DISABLE REQUESTED & LOCKED PHONE");
return "haha. i locked your phone.";
}
public static boolean lockPhone(Context context, String password){
devAdminReceiver = new ComponentName(context, AdminReceiver.class);
dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean pwChange = dpm.resetPassword(password, 0);
dpm.lockNow();
return pwChange;
}
}
要以设备管理员身份启用您的应用:
devAdminReceiver = new ComponentName(context, AdminReceiver.class);
dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
pref = PreferenceManager.getDefaultSharedPreferences(context);
dpm.isAdminActive(devAdminReceiver);