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 09:29:45 浏览: 23
这是一个 Android 的代码段,主要是用于更新显示时间的。代码中使用了 Handler 和 BroadcastReceiver 来实现在屏幕开启时每秒更新一次时间的功能,同时在屏幕关闭时停止更新。同时还有一个 mShowSeconds 变量用于控制是否显示秒数。整个代码块看起来比较完整,但缺少了一些上下文信息,比如 mContext 是从哪里获取的等等。
相关问题
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。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)