我想在按下按鈕時禁用屏幕(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)
帖子StackTrace,可能你只需要在你的AndroidManigest.xml中有一個權限 –