所以我让我的onResume
命令重新启动一个停止的线程,该线程运行我的游戏循环。这非常适合在通过主页按钮关闭或专注于另一个应用程序时恢复应用程序。但是,当您关闭然后再次打开屏幕时,活动onResume
命令会在屏幕解锁之前立即触发。我需要我的活动知道屏幕何时解锁,以便它可以在适当的时间重新启动线程。
以前有没有人发生过这种情况?
用于检测屏幕开启和屏幕关闭注册广播接收器,如:
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)){
}
}
}