所以我有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();
上述的编码似乎对设备没有任何影响,即设备不会锁定但是在设备重启之前这种情况正常。
我尝试添加日志,我确信代码已执行,但locknow()
似乎对设备没有影响......
感谢任何帮助