title: BroadcastReceiver ACTION_POWER_CONNECTED
body: <p>I've been trying to implement the simplest of things, each time you connect the device to a power resource, toast it.</p>
  
  <p>for some reason each time I connect it, the application collapses.
  And because I need to unplug and plug again i cant see the logcat so I'm quite lost on why it's happening</p>
  
  <p>My broadcastreceiver class</p>
  
  <pre><code>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();
                  }
              }
          }       
      }
    }
  </code></pre>
  
  <p>My mainActivity</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>My manifest</p>
  
  <pre><code> &lt;receiver android:name=".ChargingOnReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED"/&gt;
              &lt;action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>
  
  <p>I worked as it says here:</p>
  
  <p><a href="http://developer.android.com/training/monitoring-device-state/battery-monitoring.html" rel="nofollow noreferrer">http://developer.android.com/training/monitoring-device-state/battery-monitoring.html</a></p>