private void updateShowSeconds() { if (mShowSeconds) { // Wait until we have a display to start trying to show seconds. if (mSecondsHandler == null && getDisplay() != null) { mSecondsHandler = new Handler(); if (getDisplay().getState() == Display.STATE_ON) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); mContext.registerReceiver(mScreenReceiver, filter); } } else { if (mSecondsHandler != null) { mContext.unregisterReceiver(mScreenReceiver); mSecondsHandler.removeCallbacks(mSecondTick); mSecondsHandler = null; updateClock(); } } }private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { if (mSecondsHandler != null) { mSecondsHandler.removeCallbacks(mSecondTick); } } else if (Intent.ACTION_SCREEN_ON.equals(action)) { if (mSecondsHandler != null) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } } } }; private final Runnable mSecondTick = new Runnable() { @Override public void run() { if (mCalendar != null) { updateClock(); } mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } }; }
时间: 2024-02-10 11:30:33 浏览: 97
这段代码与前面的代码片段非常相似,它也是 Android 中用于更新时钟的代码。不同的是,它包含一个 updateShowSeconds() 方法,用于控制是否显示秒。如果 mShowSeconds 变量为 true,则会创建一个 Handler 对象,用于每隔一秒钟更新时钟。同时,还会注册一个 BroadcastReceiver,用于监测屏幕的开和关。当屏幕关闭时,会移除 Runnable 对象的回调。当屏幕开启时,会将 Runnable 对象的回调加入到 Handler 中。如果 mShowSeconds 变量为 false,则会移除 BroadcastReceiver 的注册,移除 Runnable 对象的回调,并将 Handler 对象设置为 null。updateClock() 方法用于更新时钟,mCalendar 变量用于获取当前时间。
相关问题
private void updateShowSeconds() { if (mShowSeconds) { // Wait until we have a display to start trying to show seconds. if (mSecondsHandler == null && getDisplay() != null) { mSecondsHandler = new Handler(); if (getDisplay().getState() == Display.STATE_ON) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); mContext.registerReceiver(mScreenReceiver, filter); } } else { if (mSecondsHandler != null) { mContext.unregisterReceiver(mScreenReceiver); mSecondsHandler.removeCallbacks(mSecondTick); mSecondsHandler = null; updateClock(); } } } Android4.4 编译display.getState()报错,getDisplay().getState(,使用getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().getState()也不行,如何完整修改
在 Android 4.4 中,Display 类并没有 getState() 方法,所以编译时会报错。如果你想获取当前屏幕的状态,可以使用 PowerManager 类的 isScreenOn() 方法。另外,getDisplay() 方法也需要修改,可以使用 Context 类的 getSystemService() 方法获取 WindowManager,然后再调用 getDefaultDisplay() 方法获取 Display 对象。修改后的代码如下:
private void updateShowSeconds() {
if (mShowSeconds) {
// Wait until we have a display to start trying to show seconds.
if (mSecondsHandler == null && getSystemService(Context.POWER_SERVICE) != null) {
mSecondsHandler = new Handler();
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (powerManager.isScreenOn()) {
mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
}
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
mContext.registerReceiver(mScreenReceiver, filter);
}
} else {
if (mSecondsHandler != null) {
mContext.unregisterReceiver(mScreenReceiver);
mSecondsHandler.removeCallbacks(mSecondTick);
mSecondsHandler = null;
updateClock();
}
}
}
注意,修改后的代码可能还需要进行一些测试和调试,以确保它能够正常工作。
private void updateShowSeconds() { if (mShowSeconds) { WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int state = display.getState(); // Wait until we have a display to start trying to show seconds. if (mSecondsHandler == null && display != null) { mSecondsHandler = new Handler(); if (state == Display.STATE_ON) { mSecondsHandler.postAtTime(mSecondTick, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); } IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); mContext.registerReceiver(mScreenReceiver, filter); } } else { if (mSecondsHandler != null) { mContext.unregisterReceiver(mScreenReceiver); mSecondsHandler.removeCallbacks(mSecondTick); mSecondsHandler = null; updateClock(); } } }Android4.4 编译display.getState()报错
在Android 4.4中,`getState()`方法是在`Display`类中被添加的,因此该方法仅适用于API级别21及更高版本的Android。如果您在Android 4.4中使用`getState()`方法,编译器会报错。
如果您需要在Android 4.4中使用此功能,您可以考虑使用其他方法替代,例如检查屏幕是否亮着。您可以使用以下代码来检查屏幕状态:
```
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
```
请注意,此方法仅检查屏幕是否亮着,并不提供更详细的屏幕状态信息。如果您需要更详细的信息,请考虑升级到更高版本的Android。
阅读全文