简体   繁体   中英

Android Proximity Alert - Not Triggering

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"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM