广播: 为什么一直监听不到 ACTION_POWER_DISCONNECTED
private class BatteryLevelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
Log.d(TAG, "收到了电量变化的广播 action is ---" + action);
Log.d(TAG, "当前电量 :" + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
if (textView != null) {
textView.setText("当前电量:" + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
}
} else if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
Log.d(TAG, "USB线连接上了。。。");
Toast.makeText(MainActivity.this, "USB线连接了。。。", Toast.LENGTH_SHORT).show();
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
Log.d(TAG, "USB线断开了。。。");
Toast.makeText(MainActivity.this, "USB线断开了。。。", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null) {
this.unregisterReceiver(receiver);
}
}
}
就是跟锯老师写的一毛一样的,就是监听不了 Intent.ACTION_POWER_DISCONNECTED。
用了TOAST也不显示,看了好久,代码没问题,Logcat 代替之显示的是以下信息:
2020-08-29 15:36:40.929 8478-8497/com.example.batterylevel D/EGL_emulation: eglMakeCurrent: 0xb1305060: ver 3 1 (tinfo 0xb13031e0)
打扰了各位,发了提问后不知道怎么撤销。
已经找到错误了,写成了
希望大家不要放这样粗心的错误。