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

时间:2014-08-20 16:33:50

标签: android device uninstall

请帮忙。 。我构建应用程序,但我想当用户尝试卸载mp app时,它需要密码。我试试这个,但它不起作用:
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> 

,最后在xml/deviceadmin

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

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

1 个答案:

答案 0 :(得分:0)

如果没有固件修改,这在Android中是不可能的。 请参阅AOSP代码以获取进一步的管理政策

OR

在AndroidL中推送deviceower.xml,这将使你的应用程序设备所有者因此你可以应用该策略,但先决条件是设备应该是rroted