【问题标题】:DevicePolicyManager `lockNow();` Not working After device rebootDevicePolicyManager `lockNow();` 设备重启后不工作
【发布时间】:2017-02-05 18:41:57
【问题描述】:

所以我有一个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() 似乎对设备没有影响......

感谢任何帮助

【问题讨论】:

    标签: android device-policy-manager


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2018-02-18
      相关资源
      最近更新 更多