BroadcastReceiver ACTION_POWER_CONNECTED

时间:2015-02-03 15:48:54

标签: android broadcastreceiver battery

我一直在努力实现最简单的事情,每次将设备连接到电源时,都要为它提供烤面包。

由于某些原因,每次我连接它时,应用程序都会崩溃。 因为我需要拔掉插头再插上我看不到logcat所以我很遗憾为什么会发生这种情况

我的广播接收者课程

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

1 个答案:

答案 0 :(得分:0)

您无需传递活动对象。
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();
            }
        }
    }
}