I’m working on some application that should fetch current location and after device is located in some radius should be shown application with some message. I have a few questions. I’m trying to fetch location using
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 50, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*5, 50, locationListener); addProximityAlert(LAT,LONG); private void addProximityAlert(double latitude, double longitude) { Intent intent = new Intent(this.getApplicationContext(), SOME_CLASS_HERE); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //Does this needed even if apllication/activity is alive? PendingIntent proximityIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); //and does this needed again? locationManager.addProximityAlert(latitude, longitude, Radius, 0, proximityIntent); }
If I use requestLocationUpdates then GPS is always working. And this is a waste of battery. Maybe there is some way, for example, to pause this for a several minutes? I mean use GPS every 5 minutes for example from one location fetching to next. How to realize this?
If SOME_CLASS_HERE – some activity class, then how I could know if exactly this event called it? Because when ProximityAlert is triggered I need to call some activity’s function as well. How could I do this?
UPD: Now I’m using only Activity (I haven’t Service at all) I’m trying next:
Intent intent = new Intent(getString(R.string.intent_message_location_fetched)); PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0); locationManager.addProximityAlert(LAT, LONG, RADIUS, 0, proximityIntent);
As I understand intent with action R.string.intent_message_location_fetched should be fired after location will be around LAT LONG.
Then I’m trying to create BroadcastReciver class:
public class MyBroadcastReciver extends BroadcastReceiver { MyActivity mainActivity; public MyBroadcastReciver(MyActivity activity) { mainActivity = activity; } @Override public void onReceive(Context context, Intent intent) { mainActivity.ShowToast("Recived : " + intent.getAction()); } }
And register reciver in MyActivity class:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myReciver = new MyBroadcastReciver(this); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(getString(R.string.intent_message_location_fetched)); registerReceiver(myReciver, intentFilter); ...
But nothing happens when I’m in LAT LONG location. What’s wrong? Maybe I should register reciver in AndroidManifest.xml? But how exactly?
Advertisement
Answer
1.You should check the answer here for using a timer to turn on / off the location lookups via GPS : android locationManager requestLocationUpdates
It will start a timer every X seconds. During the processing of the timer, locations updates will be requested, and after a location is retrieved, it will stop the GPS listener.
2.You should send an intent after your proximity alert logic is over. You can then register a broadcast receiver that listens for that intent, and do your processing. The Intent mechanism provides a loosely coupled way of communicating between activities/services.
You can find an example of how an intent is broadcast (in your case from your proximity alert) to a broadcastreceiver (a new component in your application that will receive the intent, and process it accordingly). It also shows how to setup the receiver in the manifest to listen for your custom Intent.