3

I am going crazy not getting what I am doing wrong and I am now turning to StackOverflow for help.

I am trying to add locations with proximity alerts and when the proximity alert triggers I get a notification. I am getting location updates when moving around and I have application continiously printing my distance to the location where my proximity alert is centered.

The issue is that the proxmity alerts never trigger. I have been using the physical device with the actual GPS when trying this.

Code for setting the proximity alert. The toast at the end is shown so I know that this code runs.

    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 the toast or the 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 dont know if it makes a difference but the location manager instance used in the application to see the distance to the proximity alert center is a different instance that is used to create the proximity alert. So the location manager instance that created the proximity alert are not requesting location updates but I am guessing that it doesn't matter.

Here are the permissions I use:

<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"/>
Flipbed
  • 719
  • 9
  • 18
  • Do you have the correct location permissions in the manifest? Also, are you able to successfully call the other LocationManager methods, like requestLocationUpdates(), successfully? – stackoverflowuser2010 Sep 25 '14 at 06:33
  • See my edit for the permissions I use. I added requestLocationUpdates() with a toast and it appeared as expected. So the LocationManager instance works. – Flipbed Sep 26 '14 at 08:14

1 Answers1

3

Did you register the receiver in Manifest?

<receiver android:name="com.example.ProximityAlertBroadcastReceiver" >
    <intent-filter>
        <action android:name="<your intent>" />
    </intent-filter>
</receiver>

See Notify users when they reach a location

Community
  • 1
  • 1
isalgueiro
  • 1,973
  • 16
  • 20
  • No, from what I understood I didnt have to if I used the context.registerReceiver method. But I will try as soon as I can and get back to you. – Flipbed Sep 26 '14 at 14:01
  • I think that with `Context#registerReceiver` the receiver gets called in the activity thread, so it will only work if that activity is up and running – isalgueiro Sep 29 '14 at 10:13
  • @isalgueiro, you are right! as soon as the activity ends de receiver is unregistered. Flipbed, have you tried it with the wifi on(you don't need to have internet)? – Dyna Oct 01 '14 at 16:14
  • Got it to work now! Apparently it takes some time from adding the event until it triggers (~1min). Thanks! – Flipbed Oct 11 '14 at 07:20