开发者

Proximity alerts are firing even I exit the set GPS location and occuring every 2 or 1 minute reccursively

开发者 https://www.devze.com 2023-01-30 06:20 出处:网络
I recently started working on proximity alerts App. This basically changes the ringtone prof开发者_开发技巧ile (just vibrate or ringtone intially) based on the users GPS location. I completed the codi

I recently started working on proximity alerts App. This basically changes the ringtone prof开发者_开发技巧ile (just vibrate or ringtone intially) based on the users GPS location. I completed the coding part. On "enter" and "exit" Proximity alerts are getting fired and the code in registered BroadcastReceiver is been executing. Till this point it is fine. Actual problem is as follows, (with my sample code lines)

I registered one broadcastreceiver for two Intent actions. Those Intent actions are declared as below, public static final String INTENT_ACTION1 = "org.droidmania.action.PROXIMITYALERT"; public static final String INTENT_ACTION2 = "org.droidmania.action.PROXIMITYALERT2";

And PendingIntent part is as,

private void setProximityAlerts(String intentAction){ Intent intent = new Intent(); intent.setAction(intentAction); //in case the call comes from HomeActivity intentAction will be INTENT_ACTION1 else INTENT_ACTION2

PendingIntent pIntent = PendingIntent.getBroascast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

locationManager.addProximityAlert(latitude,longitude, vRadius, -1,pIntent); }

Now the BroadcastReceiver code,

class ProxyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){

boolean isEnter = intent.getBooleanExtra(KEY_PROXIMITY_ENTERING,false);

if(isEnter){
  **//if user is in Home location**
   if(intent.getAction().equals(INTENT_ACTION1)){  
     give the notification that user is in home area
  }
 **//if user is in Office location**
 if(intent.getAction().equals(INTENT_ACTION2)){
     give the notification that user is in office area
 }

} else{ //if user is out of Home location

if(intent.getAction().equals(INTENT_ACTION1)){

give the notification that user is out of home area } //if user is out of Office location if(intent.getAction().equals(INTENT_ACTION2)){ give the notification that user is out of office area } } }

This is the way I tested it on my mobile device,

  1. In home I set the gps location with 10m radius. It gave me notification that "user is in home area"

  2. It shown "user exited the home area" message when I am out of that radius.

  3. Now when I am in office I set the gps location with 5m radius. So It gave me notification that "user is in office area". But here I am facing the actual problem as It is showing both "home area entered" and "office area entered" and "home area exited " and "office area exited" messages for every 2 or 1 minute(s) gap. I am not getting why it is happening. (Even I am not moving around after I set the office location GPS..just staying there in the same point......sitting in my office desk seat...)

    Am I missing or giving something extra here? Why the alerts are occuring for home proximity as it's been long time since I exited the home area?

    Guys please help me out (It's been already 2 weeks of time I spent :-( ). Your help is appreciated.

Thanks.


ProximityAlerts are stored with parameters (latitude, longitude, radius, PendingIntent), where the PendingIntent is its identifier.

In order to differentiate PendingIntents, use the PendingIntent's requestCode argument.

PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags);

So in the question's getBroadcast parameters (context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT), replace "0" with a unique integer for "home" PendingIntent and "office" PendingIntent. Then register each PendingIntent with its own lat/lon/radius in your addProximityAlert().

Failing to make each PendingIntent unique causes ProxAlert1(lat1, lon1, rad1, PendInt1) and ProxAlert2(lat2, lon2, rad2, PendInt1) to occupy the same alert space in the system's perspective. So both will be triggered at both locations. That is, you are always entering and exiting at the same time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号