【发布时间】: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