我必须使用BroadCastReceivers:一个用于从蓝牙低功耗(BLE)收听数据,另一个用于屏幕状态,如下所示:
private String sreenState="";
public class ScreenStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
sreenState="screen on";
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i(TAG, "screen on");
sreenState="screen on";
}
}
}
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
String data=intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
Log.d("TAG",sreenState);
}
}
}
在BLE的BroadcastReceiver中,我希望在screenState
发生时通过ACTION_DATA_AVAILABLE
变量显示屏幕的当前状态。但是,它始终返回屏幕的先前状态。例如,屏幕关闭,我收到数据,它将显示" null。"如果屏幕打开,我收到数据,则状态结果为关闭。我认为这是同步问题。我怎么解决呢?谢谢大家
答案 0 :(得分:1)
您还可以将BLE接收器内的当前屏幕状态读取为:
mIsScreenOn = ((PowerManager) this.getSystemService(Context.POWER_SERVICE)).isScreenOn();