Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location Manager ProxmityAlert always says entering

I want to alert the user when he is near a particular location. For this I have included a ProxmityAlert and a corresponding service in my app. But no matter what coordinates I give, it always shows me "Thank you for visiting my Area!! entering" Am I doing this the wrong way?

This is how I am doing it :

public class ProxTest extends Activity {
LocationManager lm;
double lat = 30.085514, long1 = 77.082603;
float radius = 50;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    Intent i = new Intent();
    i.setAction("com.example.test.proximityalert");

    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
            -1, i, 0);
    lm.addProximityAlert(lat, long1, radius, -1, pi);
    sendBroadcast(i);
    System.out.println("Prox alert added ");
}

}

And this is the receiver :

public class ProximityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    String k = LocationManager.KEY_PROXIMITY_ENTERING;

    boolean state = arg1.getBooleanExtra(k, false);

    if (state) {

        Toast.makeText(arg0, "Welcome to my Area", Toast.LENGTH_LONG)
                .show();
    } else {

        Toast.makeText(arg0, "Thank you for visiting my Area!!"+k,
                Toast.LENGTH_LONG).show();

    }
}
}

In the Manifest, I have added :

<receiver android:name="ProximityReceiver">
      <intent-filter>
      <action android:name="com.example.test.proximityalert">
      </action>
      </intent-filter>
  </receiver>
like image 829
Sparkplug Avatar asked May 16 '14 05:05

Sparkplug


1 Answers

In my view you should use multiple geofencing, as the simplest way to achieve what you want. You can do something like a target area. Thus the target eye is the center where you want the user to be heading off or achieving it.

enter image description here
(source: cision.com)

Like in this target image, for example you will have counting from outside to inside, a total of 6 geofences. The closest to the target, the higher the geofence ID 6, the most far away area from the target will be the geofence ID 1. Basically you will need to coordinate multiple geofencing to achieve what you wish. So, instead of having separated geofences with overlapping border areas, you will have centric geofences.

Each time your user gets a geofence higher value, an intent gives a signal or tone, such as: "you are getting closer". The other way around is also true, for the case the user is moving far away from the target area: "your geofence ID changed from 3 for 2, you are moving in the opposite direction from your target destination".

Your current code doesn't make this kind of distinction of different directions related to geofencing, neither shows handling of multiple geofencing. That's why the code is not behaving the way you would like it.

like image 175
Emma Yazzie Avatar answered Oct 23 '22 16:10

Emma Yazzie