Search code examples
androidlocationmanager

Android addProximityAlert for all application


I want show Activity when device enter the fixed zone. I have startActivity in recivier(GpsAlarmRecivier). Code below works, but when I close Activity, it crash. I know it' s because i must unregister recivier.

But I want use addProximityAlart for all application, even after close my activity(for example, move to previous). Is it possible ?

Intent myIntent = new Intent("gpsup.namespace.ProximityAlert");             


PendingIntent proximityIntent = PendingIntent.getBroadcast(cxt, 0, myIntent, 0);

locationManager.addProximityAlert(records.get(pos).x, records.get(pos).y, records.get(pos).r,  
-1, proximityIntent);

IntentFilter filter = new IntentFilter("gpsup.namespace.ProximityAlert");
actv.getApplicationContext().registerReceiver(new GpsAlarmReceiver(), filter);

I want use addProximityAlert, even if I close activity, when i created recivier. Thanks for any advices.


Solution

  • I don't believe that there is a way to directly register a system GPSBroadcastReceiver in your application. If that was the case you could just put it in your manifest and it'll get resolved when an update comes out and then you can fire off you custom intent after performing your checks.

    I believe that is actually the reason why they don't allow it (I may be wrong). It would be problematic if every application was woken up when a GPS update came out. They would be spanking the battery in the background.

    A suggestion that I can give is to create a Service that listens for your GPS updates and then Broadcasts your intents. While you can have it running in the background forever, it certainly has a longer life cycle than an Activity does.