Android:无法从服务启动管理员状态授予活动

时间:2012-06-11 12:37:10

标签: android service android-activity device-admin

我正在使用以下代码启动要求用户授予应用程序设备管理员状态的活动。

        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
        try {
            mDeviceAdminSample = new ComponentName(this, WatchDogAdmin.class);
        } catch (Exception ex) {
            // TODO Auto-generated catch block
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        }  

        Intent intent2 = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//        Intent intent2 = new Intent(this, ShuntActivity.class);
        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent2.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
        intent2.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Device admin rights are required for this application to work correctly.");
//        startActivityForResult(intent, RESULT_ENABLE);
        startActivity(intent2);

我做错了什么?没有错误,活动根本无法启动。

2 个答案:

答案 0 :(得分:0)

尝试删除该行,

intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

答案 1 :(得分:0)

确保WatchDogAdmin是接收者并尝试startActivityForResult(intent2, 1);