android:使用设备管理员锁定卸载应用程序

android:使用设备管理员锁定卸载应用程序,android,device,uninstallation,Android,Device,Uninstallation,请帮忙。我建立应用程序,但我想当用户尝试卸载mp应用程序时,它需要密码。我尝试过这个,但不起作用: 在myActivity中: AdminReceiver a = new AdminReceiver(); a.onDisableRequested(getApplicationContext(), getIntent()); AdminReceiver: public class AdminReceiver extends DeviceAdminReceiver{ static Devic

请帮忙。我建立应用程序,但我想当用户尝试卸载mp应用程序时,它需要密码。我尝试过这个,但不起作用:
myActivity
中:

 AdminReceiver a = new AdminReceiver();
 a.onDisableRequested(getApplicationContext(), getIntent());
AdminReceiver

public class AdminReceiver extends DeviceAdminReceiver{

static DevicePolicyManager dpm;
static ComponentName devAdminReceiver;

 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, "pass");
     //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;

 }
AndroidManifest
中:

 <receiver android:name=".app.AdminReceiver"
      android:label="@string/app_name"
      android:description="@string/hello_world"
      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> 

实际上我真的很困惑,它是如何工作的。请帮助我,我真的需要它来完成我的最终项目

,如果没有固件修改,在Android中是不可能的。 有关进一步的管理策略,请参阅代码

在AndroidL push deviceower.xml中,这将使您的应用程序成为设备所有者,因此您可以应用该策略,但前提是设备应被损坏

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
  <limit-password />
  <watch-login />
  <reset-password />
  <force-lock />
  <wipe-data />
  </uses-policies>
</device-admin>