2016-05-10 172 views
1

有了Android 6,我想設置的全局設置WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN,這是我做了什麼:的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

更新:

在設置權限

安卓權限=「android.permission.BIND_DEVICE_ADMIN

你的接收器與定義的策略添加到DeviceAdministration接收器:

android:resource =「@ xml/device_admin_樣本「

要激活DeviceAdministration,您必須轉到設備的Settings-> Security-> DeviceAdministration並激活它以使用管理功能。


java.lang.SecurityException異常:管理員 ComponentInfo {COM .../COM .... MyDeviceAdminReceiver}不擁有 設備

我會說你要成爲root用戶來做到這一點。我現在需要爲我的公司做同樣的事情,但我們擁有root權限。如果我有關於此的更多信息,我會稍後更新此答案。