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.
if you want your broadcast receiver to run all teh time, yu need to register it from manifest file. from documentation: You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
i think Service class will help you , try to implement .... in background for infinity times and when ever your service will find that something is changed then it will send some broadcast . to you :)
If you want this works in background then, You need to add broadcast receiver in manifest file with Broadcast receiver class.
like this tutorial http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
You can use your own "MyReceiver".
Here in this case you dont need to register and unregistered. This will work automatically.