在我的应用程序中,我需要知道设备何时被锁定(在 HTC 上,它看起来像短按“电源”按钮)。所以问题是:当设备被锁定时会触发哪个事件?或者设备要休眠了?
问问题
3540 次
3 回答
5
您应该扩展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);
于 2011-05-31T11:06:17.330 回答
3
有一个更好的方法:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}
于 2011-12-29T13:45:00.027 回答
0
除了上述答案之外,如果您想在您的应用程序处于前台时触发某些操作:
当您的应用程序从先前的静止状态(即,如果您的应用程序处于后台(暂停/最小化... )
protected void onResume()
{
super.onResume();
//call user-defined function here
}
于 2011-05-31T11:18:01.523 回答