2015-10-02 81 views
1

我想在按下按鈕時禁用屏幕(Android 4.4.2)。這可能沒有紮根設備?我試過下面的代碼,但它不起作用(它拋出異常)。如何禁用沒有root權限的Android屏幕?

private DevicePolicyManager mDPM;

OnCreate中:

mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

按鈕偵聽:

mDPM.lockNow();

的AndroidManifest.xml:

<intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" /> </intent-filter>

堆棧跟蹤:

10-03 20:32:50.187 4172-4172/com.microchip.android.BasicAccessoryDemo_API12 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.microchip.android.BasicAccessoryDemo_API12, PID: 4172 java.lang.SecurityException: No active admin owned by uid 10175 for policy #3 at android.os.Parcel.readException(Parcel.java:1465) at android.os.Parcel.readException(Parcel.java:1419) at android.app.admin.IDevicePolicyManager$Stub$Proxy.lockNow(IDevicePolicyManager.java:1594) at android.app.admin.DevicePolicyManager.lockNow(DevicePolicyManager.java:1076) at com.microchip.android.BasicAccessoryDemo_API12.PasswordActivity$1.onClick(PasswordActivity.java:209) at android.view.View.performClick(View.java:4442) at android.view.View$PerformClick.run(View.java:18471) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method)

+0

帖子StackTrace,可能你只需要在你的AndroidManigest.xml中有一個權限 –

回答

0

我發現其可以鎖定該設備(如果它有碼保護),因此禁止屏幕的碼。以下是代碼。

在清單把下面的代碼:

<receiver 
     android:name=".MyDeviceAdminReceiver" 
     android:permission="android.permission.BIND_DEVICE_ADMIN" > 
     <meta-data 
      android:name="android.app.device_admin" 
      android:resource="@xml/my_admin" /> 

     <intent-filter> 
      <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
     </intent-filter> 
    </receiver> 

將這個宣言中的onCreate的活動:

static final int RESULT_ENABLE = 1;

把下面的代碼在按鈕偵聽:

// Enable the Administrator mode. 
DevicePolicyManager deviceManger = (DevicePolicyManager) getSystemService(
     Context.DEVICE_POLICY_SERVICE); 
ActivityManager activityManager = (ActivityManager) getSystemService(
     Context.ACTIVITY_SERVICE); 
ComponentName compName = new ComponentName(getApplicationContext(), 
     MyDeviceAdminReceiver.class); 
Intent device_policy_manager_Int = new Intent(DevicePolicyManager 
     .ACTION_ADD_DEVICE_ADMIN); 
device_policy_manager_Int.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, 
     compName); 
device_policy_manager_Int.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, 
     "Additional text explaining why this needs to be added."); 
startActivityForResult(device_policy_manager_Int, RESULT_ENABLE); 

// Check if the Administrator is enabled. 
boolean active = deviceManger.isAdminActive(compName); 

if (active) { 
    Log.i("isAdminActive", "Admin enabled!"); 

    // If admin is enable - Lock device. 
    deviceManger.lockNow(); 
} 
相關問題