title: Android BroadcastReceiver: context issue
body: <p>I create an application that implements simple geolocation problem: once in, say, 20 minutes, it takes a LatLng coordinate.</p>
  
  <p>For this purpose, from <code>MainActivity</code>, I initiate <code>BroadcastReceiver</code> to work. It instantiates <code>LocationManager</code> to find coordinates, which needs <em>application context</em>.</p>
  
  <p>The <strong>problem</strong> is: due to memory reasons, Android OS can kill my <code>MainActivity</code>, so, <code>BroadcastReceiver</code>, firing next time, catches null pointer exception, referring to <em>application's context</em>.</p>
  
  <p><strong>Ideas</strong>:</p>
  
  <p>I. I could restart the activity inside <code>BroadcastReceiver</code> like this:</p>
  
  <pre><code>@Override
  public void onReceive(Context context, Intent intent) {
      //start activity
      Intent i = new Intent();
      i.setClassName("com.test", "com.test.MainActivity");
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(i);
  }
  </code></pre>
  
  <p>but <em>context</em> is null due to killed Activity.</p>
  
  <p>II. Maybe, the paradigm, presented in my solution, too cumbersome?
  Maybe here is <em>graceful</em> solution, I even didn't think of?</p>
  
  <p>Well, my <strong>code</strong> snippet:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  // ...
  
      public void onStartSessionButtonClicked (View view) {
          Intent alarmRecIntent = new Intent(this, AlarmReceiver.class);
          PendingIntent mAlarmIntent = PendingIntent.getBroadcast(this, 0, alarmRecIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);
          alarmManager.cancel(mAlarmIntent);
          alarmManager.setInexactRepeating(
                  AlarmManager.ELAPSED_REALTIME,
                  SystemClock.elapsedRealtime() + 1000,
                  RConstants.locUpdateInterval,
                  mAlarmIntent
                  );
      }
  }
  
  public class AlarmReceiver extends BroadcastReceiver {
      @Override
      public void onReceive (Context context, Intent intent) {
          Context mContext = context;
          try {
              LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
              if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                  throw new Exception("network provider is not enabled");
              }
              locationManager.requestLocationUpdates(
                      LocationManager.NETWORK_PROVIDER,
                      MIN_TIME_BW_UPDTS,
                      MIN_DIST_CHANGE_FOR_UPDTS,
                      locationListener
              );
          }
          catch (Exception e) { /* catch codeblock */ }
      }
  </code></pre>