1

所以我有一个BroadcastReceiver注册为:

<receiver
        android:name="package.MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="999">
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>

DevicePolicyManager一旦用户解锁设备,我就使用它来锁定设备 -MyBroadcastReceiver收到广播,请参见下面的代码:

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


    deviceManger = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(startMain);

                    deviceManger = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
                    //also tried with context..getApplicationContext() but no luck
                    deviceManger.lockNow();
                }
            }
        };

}

所以这段代码工作正常,但是当设备关闭并重新启动时:

deviceManger = (DevicePolicyManager) context.getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
                    deviceManger.lockNow();

上面的 coed 似乎对设备没有影响,即设备不会锁定,但这在设备重新启动之前工作正常。我尝试添加日志,我确信代码会被执行,但locknow()似乎对设备没有影响......

任何帮助表示赞赏

4

1 回答 1

0

我发现它在前一两次不起作用,但在那之后开始工作......

于 2017-02-24T06:52:06.997 回答