Search code examples
androidscreen-lock

How disable screen in Android without root?


I want to disable the screen (Android 4.4.2) when I press a button. Is that possible without rooting the device? I have tried the code below, but it 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 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)


Solution

  • I found a code which can lock the device (if it has code protection) and consequently to disable the screen. Below is the code.

    On the Manifest put the 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 this declaration in onCreate's Activity:

    static final int RESULT_ENABLE = 1;

    Put the below code in Button Listener:

    // 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( );
    }