android:卸载应用程序时需要密码

vir*_*rho 0 authentication passwords android uninstallation

我想构建像家长控制这样的应用程序,所以当孩子尝试卸载/删除我的应用程序时,我想要求用户在被允许卸载/删除我的应用程序之前输入密码。

我尝试了这个,但仍然不明白:
需要密码才能卸载/删除应用程序

有什么建议吗?

Ree*_*eed 5

如果您使用设备管理,您可以锁定设备。用户无法卸载活动的设备管理员,如果他们尝试禁用设备管理员,您可以锁定设备,然后父母可以输入密码将其解锁。

警告:如果您的用户不是很清楚这是如何工作的,这可能被认为是恶意的。如果不允许这种行为,请查看您发布的任何应用商店的条款和条件。

在您的清单中:

  <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>
Run Code Online (Sandbox Code Playgroud)

然后在 @xml/deviceadmin

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <reset-password />
    <force-lock />
  </uses-policies>
</device-admin>
Run Code Online (Sandbox Code Playgroud)

然后

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;
    }   
}
Run Code Online (Sandbox Code Playgroud)

以设备管理员身份启用您的应用程序:

devAdminReceiver = new ComponentName(context, AdminReceiver.class);
        dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        pref = PreferenceManager.getDefaultSharedPreferences(context);
        dpm.isAdminActive(devAdminReceiver);
Run Code Online (Sandbox Code Playgroud)