• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps LocationListener within a BroadcastReceiver: how to close it after the onRecieve's done

frysay

Lurker
Apr 2, 2014
1
0
I actually have 3 questions to ask. Don't get scared from the lenght of the topic. The answers should be quite short.

1 - I'll first show you the code and then ask the question so that it should be easier to get what my problem is:

First Class:

public class ProxAlertActivity extends Activity {

..........

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

.......
}

.........

private void saveProximityAlertPoint() {

............

ProximityIntentReceiver pir = new ProximityIntentReceiver();
pir.addProximityAlert(this, location, ++requestcode, onOff.isChecked());
}
}
Second Class:

public class ProximityIntentReceiver extends BroadcastReceiver implements LocationListener{

...........

@Override
public void onReceive(Context context, Intent intent) {

..........

this.removeProximityAlert();

.........
}

public void addProximityAlert(Context context, Location location, int requestcode, boolean message) {

............

locationManager.requestLocationUpdates(getProvider(), MINIMUM_TIME_BETWEEN_UPDATE, MINIMUM_DISTANCECHANGE_FOR_UPDATE, this);

Intent intent = new Intent(context, ProximityIntentReceiver.class);
intent.putExtra("com.example.proximityalert.ProxAlertActivity.latitude", finalLocation.getLatitude());
intent.putExtra("com.example.proximityalert.ProxAlertActivity.longitude", finalLocation.getLongitude());
intent.putExtra("com.example.proximityalert.ProxAlertActivity.id", id);
intent.putExtra("com.example.proximityalert.ProxAlertActivity.message", message);
PendingIntent proximityIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

locationManager.addProximityAlert(finalLocation.getLatitude(), finalLocation.getLongitude(), POINT_RADIUS, PROX_ALERT_EXPIRATION, proximityIntent);
}

..........

public void removeProximityAlert() {
Intent intent = new Intent(context, ProximityIntentReceiver.class);
PendingIntent proximityIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

.........

locationManager.removeProximityAlert(proximityIntent);
locationManager.removeUpdates(this);
}

.........

public void onLocationChanged(Location location) {

........

}
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}

"this" is refered to LocationListener. The thing is how do I close the LocationListener? I tried with locationManager = null; and myLocationListener = null; but nothing happened. I thought the problem was that when I first use "this" to add the proximity alert I should close it before the onReceive was triggered since the "this" called by addProximityAlert it's not actually the same "this" called by removeProximityAlert that's called by the onReceive. So I tried to close both but it doesn't work. Everything works fine and the class does what it has to do but I keep having the gps icon on the top of the screen blinking that means it's still listening. I know it seems a bit messy but it's not. I hope the code makes it much clear than my explanation. You could wonder why I used the BroadcastReceiver like that. The answer brings us to the second question:

2 - I need to set multiple proximity alert and all of them should work in the same way but with different parameters. So the thing is, when I'm able to close the LocationManager and the LocationListener if I close it for just one instance of BroadcastReceiver does it close all the other LocationManagers and LocationListeners? It should not according with java I mean right? And is it possible to use different requestUpdates based on different parameters even if they are all with the same provider? Like:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time1, distance1, this);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time2, distance2, this);

You could wonder why. The thing is that I have a function to estimate time and distance and if I can't do this I should make a new function that considers all of these variables to get a common time and distance that fit all of the updates. Believe me it would be a real mess mathematically speaking.

3 - Last question that could be actually a fix to all the problems above as well is: is there a way to pass the LocationListener with put in extras? Or is there a way to make a LocationListener unique like bind it with an id so that I can use that specific LocationLsitener among all the others I've istanciated?

Thanks for any answer to any of the questions. For those who would like to tell me delete and start over using the google maps api I'd like you won't give me this answer. Since this is more about cv than developping I'd like to show my own ideas and my own way of programming instead of using already made stuff. Thanks again.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones