如何揭示该屏幕被锁定?
答
您应该扩展BroadcastReceiver
和实施onReceive
,像这样:
public class YourBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
//screen has been switched off!
}
}
}
然后你只需要注册登记,当屏幕关闭时,你会开始接收事件:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
appBroadcastReceiver = new AppBroadcastReceiver(yourActivity);
registerReceiver(appBroadcastReceiver, filter);
答
除了上述答案之外,如果您希望在应用处于前景时触发某些操作:
您可以使用事件称为的onResume()触发自己的功能,当你的应用程序需要的焦点从之前休息状态,也就是说,如果您的应用程序在后台(暂停/最小化......)
protected void onResume()
{
super.onResume();
//call user-defined function here
}
答
有一个更好的办法:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if(myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}
+0
你尝试过isDeviceLocked为Android 5.1 http://developer.android.com/reference/android/app/KeyguardManager.html#isDeviceLocked()? – 2015-04-03 10:05:21
看起来太棒了!我会马上查看! – barmaley 2011-05-31 11:25:44
是的,它的工作原理!非常感谢! – barmaley 2011-05-31 15:42:53
@barmaley乐于帮助! – pandre 2011-05-31 17:37:50