title: Android broadcastreceiver not triggering when app is not running
body: <p>Following this guide <a href="https://developer.android.com/training/monitoring-device-state/battery-monitoring.html" rel="nofollow noreferrer">https://developer.android.com/training/monitoring-device-state/battery-monitoring.html</a> </p>
  
  <p>I made a receiver that should log battery info to a file every time charger is plugged or unplugged. The activity reads the content of this file and displays it on screen. This is supposed to work always regardless of the app running, but it doesn't. It only registers if the app is open/in memory, when I clear the memory or reboot it stops working.</p>
  
  <p>Manifest: </p>
  
  <pre><code>  &lt;receiver android:name=".Receiver"&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>Receiver: </p>
  
  <pre><code>public class Receiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
        Intent chargingIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        final int status = chargingIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        int level = chargingIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        Battery battery = new Battery();
        battery.setLevel(level);
        battery.setDate(new Date());
        battery.setPlugged(plugged);
        battery.setStatus(status);
        Logger.log(JSON.toJSONString(battery));
      }
  }
  </code></pre>
  
  <p>Logger </p>
  
  <pre><code>public class Logger {
  
  public static void log(String text)
  {
      File logFile = new File(Environment.getExternalStorageDirectory().getPath()+"/batterylog.txt");
      Log.d("files"," logfile "+logFile+": "+logFile.exists());
  
      if (!logFile.exists())
      {
          try
          {
              logFile.createNewFile();
              Log.d("files","new logfile created"+logFile);
          }
          catch (IOException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      try
      {
          //BufferedWriter for performance, true to set append to file flag
          BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
          buf.append(text).append("\r\n");
          buf.flush();
          buf.close();
      }
      catch (IOException e)
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>This is what it looks like after unplugging while app is open: </p>
  
  <p><a href="https://i.stack.imgur.com/JfgNp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JfgNp.png" alt="enter image description here"></a></p>
  
  <p>When it's not it's just empty.</p>