Android 6,设置全局设置WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN

时间:2016-05-10 08:26:28

标签: android

使用Android 6,我想设置全局设置WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN,这就是我所做的:

mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
deviceAdminComponentName = new ComponentName(this, MyDeviceAdminReceiver.class);
try {
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,deviceAdminComponentName);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Your boss told you to do this");
    startActivityForResult(intent, ACTIVATION_REQUEST);

}catch (Exception e){
    Logg.d("Error: " + e.getMessage());
}

其中MyDeviceAdminReceiver是一个扩展DeviceAdminReceiver的类。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
    case ACTIVATION_REQUEST:
        if (resultCode == Activity.RESULT_OK) {

            try {
                mDevicePolicyManager.setGlobalSetting(deviceAdminComponentName, Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, "1");

             }catch (Exception e){
                Logg.e("Error " + e.getMessage());
             }

        } 
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


<receiver android:name=".MyDeviceAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
              android:resource="@xml/device_admin_sample" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
        <disable-camera />
        <set-global-proxy/>
        <disable-keyguard-features/>
    </uses-policies>
</device-admin>

不幸的是,在运行上一代码时,我总是在执行mDevicePolicyManager.setGlobalSetting

时遇到安全异常
java.lang.SecurityException: Admin ComponentInfo{com..../com....MyDeviceAdminReceiver} does not own the device
at android.os.Parcel.readException(Parcel.java:1627)
at android.os.Parcel.readException(Parcel.java:1579)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setGlobalSetting(IDevicePolicyManager.java:7150)
at android.app.admin.DevicePolicyManager.setGlobalSetting(DevicePolicyManager.java:4558)
at ....WifiScanActivity.onActivityResult(WifiScanActivity.java:152)
at android.app.Activity.dispatchActivityResult(Activity.java:6490)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3794)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3841)
at android.app.ActivityThread.access$1400(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

更新

设置权限时

  

机器人:权限=“android.permission.BIND_DEVICE_ADMIN

对于接收器,您将接收器添加到DeviceAdministration,并在以下位置定义策略:

  

机器人:资源= “@ XML / device_admin_sample”

要激活设备管理,您必须转到设备的设置 - >安全性 - >设备管理并激活它以使用管理功能。

  

java.lang.SecurityException:Admin   ComponentInfo {com .... / com .... MyDeviceAdminReceiver}不拥有   设备

我会说你必须是root用户才能做到这一点。我现在需要为我的公司做同样的事情,但我们拥有root权利。如果我有更多相关信息,我会在稍后更新这个答案。