0

I recently started working on proximity alerts App. This basically changes the ringtone profile (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.

4

1 回答 1

2

ProximityAlerts 与参数(纬度、经度、半径、PendingIntent)一起存储,其中 PendingIntent 是其标识符。

为了区分 PendingIntent,请使用 PendingIntent 的 requestCode 参数。

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

因此,在问题的 getBroadcast 参数(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)中,将“0”替换为“home”PendingIntent 和“office”PendingIntent 的唯一整数。然后在你的addProximityAlert().

未能使每个 PendingIntent 唯一会导致 ProxAlert1(lat1, lon1, rad1, PendInt1) 和 ProxAlert2(lat2, lon2, rad2, PendInt1) 在系统的视角中占据相同的警报空间。因此,两者都将在两个位置触发。也就是说,您总是同时进入和退出。

于 2011-11-17T21:56:28.390 回答