广播接收器 ACTION_POWER_CONNECTED
BroadcastReceiver ACTION_POWER_CONNECTED
我一直在尝试实现最简单的事情,每次将设备连接到电源时,为它干杯。
出于某种原因,每次我连接它时,应用程序都会崩溃。
因为我需要拔掉再插上,所以我看不到 logcat 所以我完全不知道为什么会这样
我的广播接收器class
public class ChargingOnReceiver extends BroadcastReceiver {
private Activity activity;
public ChargingOnReceiver(Activity activity){
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(isCharging){
if(usbCharge){
Toast.makeText(activity, "Your battery are now Charging Via USB" , Toast.LENGTH_SHORT).show();
}else{
if(acCharge){
Toast.makeText(activity, "Your battery are now Charging Via AC Plug" , Toast.LENGTH_SHORT).show();
}
}
}
}
}
我的主要活动
private ChargingOnReceiver battery;
protected void onDestroy() {
unregisterReceiver(battery);
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this,R.xml.search_settings_screen,false);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(battery, ifilter);
}
我的清单
<receiver android:name=".ChargingOnReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
我按照这里所说的工作:
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
您不需要传递 activity 对象。
onReceive() 具有所需的上下文,使用它并且 toast 将出现
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
System.out.println("isCharging:" + isCharging);
System.out.println("chargePlugType:" + chargePlug);
if (isCharging) {
if (usbCharge) {
Toast.makeText(context, "Your battery are now Charging Via USB",
Toast.LENGTH_SHORT).show();
} else {
if (acCharge) {
Toast.makeText(context,
"Your battery are now Charging Via AC Plug",
Toast.LENGTH_SHORT).show();
}
}
}
}
我一直在尝试实现最简单的事情,每次将设备连接到电源时,为它干杯。
出于某种原因,每次我连接它时,应用程序都会崩溃。 因为我需要拔掉再插上,所以我看不到 logcat 所以我完全不知道为什么会这样
我的广播接收器class
public class ChargingOnReceiver extends BroadcastReceiver {
private Activity activity;
public ChargingOnReceiver(Activity activity){
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(isCharging){
if(usbCharge){
Toast.makeText(activity, "Your battery are now Charging Via USB" , Toast.LENGTH_SHORT).show();
}else{
if(acCharge){
Toast.makeText(activity, "Your battery are now Charging Via AC Plug" , Toast.LENGTH_SHORT).show();
}
}
}
}
}
我的主要活动
private ChargingOnReceiver battery;
protected void onDestroy() {
unregisterReceiver(battery);
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this,R.xml.search_settings_screen,false);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(battery, ifilter);
}
我的清单
<receiver android:name=".ChargingOnReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
我按照这里所说的工作:
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
您不需要传递 activity 对象。
onReceive() 具有所需的上下文,使用它并且 toast 将出现
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
System.out.println("isCharging:" + isCharging);
System.out.println("chargePlugType:" + chargePlug);
if (isCharging) {
if (usbCharge) {
Toast.makeText(context, "Your battery are now Charging Via USB",
Toast.LENGTH_SHORT).show();
} else {
if (acCharge) {
Toast.makeText(context,
"Your battery are now Charging Via AC Plug",
Toast.LENGTH_SHORT).show();
}
}
}
}