Android Proximity Alert - Not Triggering
I'm going crazy and don't understand what I am doing wrong and now I turn to StackOverflow for help.
I am trying to add locations with proximity alerts and when the proximity alert fires I get a notification. I get location updates as I move and I have an app that constantly prints my distance to the location where my proximity alert is focused.
The problem is that proxy warnings are never fired. I used a physical device with actual GPS when trying this.
Code to set proximity alert. The toast is shown at the end, so I know this code works.
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(PROXIMITY_ITEM_ID, item.getID());
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, item.getID(), intent, 0);
locationManager.addProximityAlert(
item.getPlace().getLocation().latitude, // the latitude of the central point of the alert region
item.getPlace().getLocation().longitude, // the longitude of the central point of the alert region
200, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(new ProximityAlertBroadcastReceiver(), filter);
Toast.makeText(this, "Added new proximity alert event...", Toast.LENGTH_SHORT).show();
And then I have the code for the broadcast receiver. This code never runs, so I never get a toast or notification.
public class ProximityAlertBroadcastReceiver extends BroadcastReceiver {
private void createNotification(Context context, ToDoItem todoItem) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.todomaps_icon)
.setContentTitle(todoItem.getTask())
.setContentText("You are " + todoItem.getDistanceString() + " to todo: " + todoItem.getTask());
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ON RECEIVE!", Toast.LENGTH_SHORT).show(); //TODO: TEMP
int itemID = intent.getIntExtra(AddItemActivity.PROXIMITY_ITEM_ID, -1);
DBHandler handler = new DBHandler(context);
Item item = handler.getItem(itemID);
createNotification(context, item);
}
}
I don't know if this matters, but the location manager instance used in the app to view the distance to the proximity alert center is a different instance that is used to generate the proximity alert. So the location manager instance that generated the proximity alert does not ask for location updates, but I guess it doesn't matter.
Here are the permissions I'm using:
<permission
android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
source to share