屏幕解锁时的活动句柄
问题描述:
所以我有我的onResume
命令重新启动一个停止的线程运行我的游戏循环。 这适用于在主页按钮关闭或重点关注其他应用程序时恢复应用程序。 但是,当您关闭屏幕然后再次打开时,在屏幕解锁之前,活动onResume
命令将立即触发。我需要我的活动来了解屏幕何时解锁,以便它可以在适当的时间重新启动线程。屏幕解锁时的活动句柄
有没有人有过这种情况?
答
用于检测屏幕上,屏幕关闭寄存器的广播reciver像:
的AndroidManifest.xml:
<receiver android:name="receiverScreen">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
在活动或服务:
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new receiverScreen();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
接收机如果屏幕开启/关闭发生,系统会通知您的代码为:
public class receiverScreen extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
}
}
看起来像AndroidManifest.xml的代码没有包含在内。 – louielouie 2012-03-17 05:47:55
非常有帮助,但ACTION_SCREEN_OFF只在屏幕开启并且解锁屏幕出现时执行,当解锁屏幕解锁并消失时,我需要执行此操作 – tantonj 2012-03-17 08:51:49
@tantonj然后注册更多BroadcastReceiver接收器'Intent.ACTION_USER_PRESENT' – 2012-03-17 08:58:19