簡體   English   中英

android:卸載應用程序時需要密碼

[英]android: require password when uninstall app

我想構建像家長控制這樣的應用程序,所以當孩子嘗試卸載/刪除我的應用程序時,我想要求用戶在被允許卸載/刪除我的應用程序之前輸入密碼。

我試試這個,但還是不明白:
需要密碼才能卸載/刪除應用程序

有什么建議嗎?

如果您使用設備管理,您可以鎖定設備。 用戶無法卸載活動的設備管理員,如果他們嘗試禁用設備管理員,您可以鎖定設備,然后父母可以輸入密碼將其解鎖。

警告:如果您的用戶不是很清楚這是如何工作的,這可能被認為是惡意的。 如果不允許這種行為,請查看您發布的任何應用商店的條款和條件。

在您的清單中:

  <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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM