Android: The addProximityAlert LocationManager method does not send broadcasts
I’m working on an Android project and I’m using the AddProximityAlert method because you already know that this method lets you set proximity alerts for a location (latitude, longitude) and a given radius for a given location and notify if you’re so close to it.
I’ve been working on this issue three days ago but I’m having the same problem again and again…
in bref: Here is my simple code.
#MainActivity.java
package com.example.proximityalert;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity implements LocationListener {
LocationManager lm;
Defining Latitude & Longitude
double lat=37.422006 ,long1=-122.084095;
Defining Radius
float radius=1000;
Intent Action
String ACTION_FILTER = "com.example.proximityalert";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i'm registering my Receiver First
registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));
i'm calling ther service Location Manager
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
for debugging...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
Setting up My Broadcast Intent
Intent i= new Intent(ACTION_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
setting up proximituMethod
lm.addProximityAlert(lat, long1, radius, -1, pi);
}
@Override
just For debugging to See the distance between my actual position and the aproximit point
public void onLocationChanged(Location newLocation) {
Location old = new Location("OLD");
old.setLatitude(lat);
old.setLongitude(long1);
double distance = newLocation.distanceTo(old);
Log.i("MyTag", "Distance: " + distance);
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
#ProximityReceiver.java
package com.example.proximityalert;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class ProximityReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Key for determining whether user is leaving or entering
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Gives whether the user is entering or leaving in boolean form
boolean state = intent.getBooleanExtra(key, false);
if(state){
Call the Notification Service or anything else that you would like to do here
Log.i("MyTag", "Welcome to my Area");
Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
}else{
Other custom Notification
Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
}
}
}
*#问题是当我运行程序时, BroadcastReceiver(ProximityReciever) is never called by the system, even if I’m very close to the approach point, even if the debugger shows me that the distance between the two locations is < 1000 meters :/
Solution
I
just figured out a few things about this topic and why addProximityAlert sames isn’t working, and I’m sharing this with you because I’ve noticed that someone asked the same question before but they didn’t get any answers!
The answer was right in front of me, but I didn’t pay attention to it, so when I read the official Android documentation (here), I saw the phrase “Due to the nature of the approximate position estimation, if the device briefly passes through a given area, it may not trigger any intents“
What does that mean? This means that when you test your application on AVD and you send GPS coordinates (latitude, longitude) from DDMS to AVD, it’s really hard
Emulates the real aspects of GPS, (because first you choose some points as your proximPoint, then you choose anchors far away from the proximPoint to see if it works) and that’s not the thing it’s with the real device.
So the solution is to test your application on a real device or use DDMS to try to change the coordinates very slowly until you are in the desired area.