在广播接收器中锁定Android手机

时间:2017-03-28 05:47:37

标签: android broadcastreceiver lockscreen android-broadcastreceiver

我正在使用演示应用程序来锁定Android屏幕,同时它与充电器连接。我已经尝试了很多关于这个问题的stackoverflow答案,但没有得到如何将手机完全锁定在接收器中。

这是我的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();

        }
    }
}

我收到此例外

 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) 

任何提示/教程将不胜感激。

2 个答案:

答案 0 :(得分:1)

从您的日志中:  引起:java.lang.SecurityException:uid 10143没有为策略#3拥有的活动管理员

您必须在设备中提供设备管理员权限(以锁定屏幕)

答案 1 :(得分:0)

这可以帮助你。

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

}