在我的代码中,我试图了解设备锁定时启动的意图。
例如:假设我的活动正在运行,并且我按下电源按钮来锁定手机。INTENT.ACTION_SCREEN_OFF 启动。活动暂停,屏幕变为空白。
所以我想要的是每当设备被用户锁定时,我不想完成我的活动,但是如果用户按下主页按钮或后退按钮,我正在完成活动。因此,为此我编写了一个代码,但它不起作用。
我用过的代码
public void onResume()
{
super.onResume();
m_ScreenLocked = false;
// Intent and receiver to listen to screen lock
m_IntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
m_IntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
m_BroadcastReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(m_BroadcastReceiver, m_IntentFilter);
}
private final class ScreenStateBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
m_ScreenLocked = true;
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
//other stuff
}
}
}
@Override
public void onStop()
{
super.onStop();
m_NoteManagerObject.setPageViewRunning(false);
if (!m_ScreenLocked)
{
unregisterReceiver(m_BroadcastReceiver);
finish();
}
else
{
unregisterReceiver(m_BroadcastReceiver);
}
}