我正在尝试在充电状态发生变化时在特定活动中显示警报。 这是我的接收器类:
public class BatteryChargeReceiver extends BroadcastReceiver {
private String TAG = "receiver";
private ChargeReceiverCallback callback;
public BatteryChargeReceiver(ChargeReceiverCallback callback) {
this.callback =callback;
Log.v(TAG,"Inside Constructor (Callback)");
}
public interface ChargeReceiverCallback {
public void onReceive(Intent intent);
}
@Override
public void onReceive(Context context, Intent intent) {
callback.onReceive(intent);
}
}
以下是我从MainActivity的onResume实例化它的方法。 MainActivity实现我的界面并覆盖onReceive(intent)。我发现在任何阶段都没有intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1)返回任何其他内容然后-1。
BatteryChargeReceiver batteryReceiver = new BatteryChargeReceiver(this);
IntentFilter filterPower = new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");
registerReceiver(batteryReceiver, filterPower);
答案 0 :(得分:3)
Intent.ACTION_POWER_CONNECTED
没有提供当前的插件状态 - 这可能是API工作方式的变化,但我无法在文档中找到任何提及此类更改的内容。
广播Intent.ACTION_POWER_CONNECTED
从未打算接收当前的插件类型,但它只是让电源连接的接收端(ref.)。实际上,BatteryService.java
在没有任何额外值的情况下播放普通意图(截至撰写本文时为410~412行):
Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
但是,请使用
代码<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
在Nexus 5上工作:
public class MainActivity extends Activity {
private static final String TAG = "PowerReceiver";
private final BroadcastReceiver mPowerReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
Log.d(TAG, "Power connected");
checkPluggedState(context, intent);
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
Toast.makeText(context, "Power disconnected", Toast.LENGTH_SHORT).show();
} else {
Log.wtf(TAG, "Unknown action: " + action);
}
}
};
private void checkPluggedState(Context context, Intent intent) {
Intent chargingIntent = registerReceiver(null, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
final int pluggedState = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
final String msg;
switch (pluggedState) {
case 0:
msg = "The device is running on battery";
break;
case BatteryManager.BATTERY_PLUGGED_AC:
msg = "Plugged into AC charger";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
msg = "Plugged into USB charger";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
msg = "Plugged into wireless charger";
break;
default:
msg = "Unknown state: " + pluggedState;
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(mPowerReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mPowerReceiver);
}
}
希望这有帮助。
答案 1 :(得分:2)
根据The docs,EXTRA_STATUS适用于ACTION_BATTERY_CHANGED