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); } }; } Android4.4编译报错getDisplay().getState(
时间: 2024-03-16 18:40:57 浏览: 44
根据您提供的代码,可能是因为在Android4.4中没有`getDisplay().getState()`这个方法而导致编译报错。可以尝试使用`getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().getState()`来获取屏幕状态。请修改代码如下:
```
private void updateShowSeconds() {
if (mShowSeconds) {
// Wait until we have a display to start trying to show seconds.
if (mSecondsHandler == null && getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay() != null) {
mSecondsHandler = new Handler();
if (getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().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);
}
};
```
阅读全文