Adnan Bin Mustafa
Adnan Bin Mustafa

Reputation: 1215

Lock android phone in broadcast receiver

I am working on demo app to lock android screen while its connected with charger. I have tried many stackoverflow answers on this problem, But not getting how to exactly lock cell phone in receiver.

Here is my BroadCatReceiver

public class PlugInControlReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        /**
         *Do something when power disconnected.
         */
        if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
            Toast.makeText(context, "Charger connected!", Toast.LENGTH_SHORT).show();

            //Get the window from the context
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

            //Lock device
            DevicePolicyManager mDPM = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);

            mDPM.lockNow();

        }
        /**
         * Do something when power connected
         */
        else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
            Toast.makeText(context, "Charger disconnected!", Toast.LENGTH_SHORT).show();

        }
    }
}

I am receiving this exception

 java.lang.RuntimeException: Unable to start receiver com.addy.plugin.receiver.PlugInControlReceiver: java.lang.SecurityException: No active admin owned by uid 10143 for policy #3
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    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)

Caused by: java.lang.SecurityException: No active admin owned by uid 10143 for policy #3
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.os.Parcel.readException(Parcel.java:1573)
    at android.app.admin.IDevicePolicyManager$Stub$Proxy.lockNow(IDevicePolicyManager.java:2976)
    at android.app.admin.DevicePolicyManager.lockNow(DevicePolicyManager.java:1846)
    at com.addy.plugin.receiver.PlugInControlReceiver.onReceive(PlugInControlReceiver.java:37)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    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) 

any hint/tutorial will be appreciated.

Upvotes: 0

Views: 1037

Answers (2)

SaravInfern
SaravInfern

Reputation: 3388

From your logs: Caused by: java.lang.SecurityException: No active admin owned by uid 10143 for policy #3

You must provide device admin privilege in your device (to lock the screen)

Upvotes: 1

Shubham Sejpal
Shubham Sejpal

Reputation: 3614

May this helps you.

public class MyReceiver extends BroadcastReceiver {
    private boolean scrOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            scrOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            scrOff = false;
        } else if(intent.getAction().equals(Intent.ACTION_ANSWER)) {

        }

        Intent i = new Intent(context, YourService.class);
        i.putExtra("scr_state", scrOff);
        context.startService(i);
    }

}

Upvotes: 0

Related Questions