How disable screen in Android without root? -
i want disable screen (android 4.4.2) when press button. possible without rooting device? have tried code below, doesn't work (it throws exception).
private devicepolicymanager mdpm;
oncreate:
mdpm = (devicepolicymanager)getsystemservice(context.device_policy_service);
button listener:
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>
stack trace:
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 uid 10175 policy #3 @ android.os.parcel.readexception(parcel.java:1465) @ android.os.parcel.readexception(parcel.java:1419) @ android.app.admin.idevicepolicymanager$stub$proxy.locknow(idevicepolicymanager.java:1594) @ android.app.admin.devicepolicymanager.locknow(devicepolicymanager.java:1076) @ com.microchip.android.basicaccessorydemo_api12.passwordactivity$1.onclick(passwordactivity.java:209) @ android.view.view.performclick(view.java:4442) @ android.view.view$performclick.run(view.java:18471) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:790) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:606) @ dalvik.system.nativestart.main(native method)
i found code can lock device (if has code protection) , consequently disable screen. below code.
on manifest put below code:
<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>
put declaration in oncreate's activity:
static final int result_enable = 1;
put below code in button listener:
// enable 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 needs added." ); startactivityforresult( device_policy_manager_int, result_enable ); // check if administrator enabled. boolean active = devicemanger.isadminactive(compname); if ( active ) { log.i("isadminactive", "admin enabled!"); // if admin enable - lock device. devicemanger.locknow( ); }
Comments
Post a Comment