Hi i am using a broadcast receiver for location listening but this broadcast receiver is working only when activity is in foreground else when i am not using this app then receiver is not active here is code of activity onCreate's method where i am registering my broadcast receiver
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
1000, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s);
registerReceiver(new ProximityAlertReceiver(), filter);
and code for BroadcastReceiver is
public class ProximityAlertReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
SharedPreferences shared=context.getSharedPreferences(MainActivity.PREFS_NAME,0);
Boolean entering = intent.getBooleanExtra(key, false);
Double longitude=intent.getDoubleExtra("longitude", 0.0);
Double latitude=intent.getDoubleExtra("latitude",0.0);
Intent in = new Intent(context,AlarmActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("longitude", longitude);
in.putExtra("latitude", latitude);
Log.i("cont1", shared.getInt("count", 0)+"");
in.putExtra("count",shared.getInt("count", 0));
context.startActivity(in);
}
}
i read some where that register and unregister it in onResume and onPause method respectively but when i unregister it in onPause method then it get unregistered means it will not work i want my broadcast receiver to work for all the time.can you please suggest how to accomplish this.