my code uses gps coordinates to locate and save the location.
when the person enters the area, it should create a notification and alert.
problem: when the user saves the location, it sends an alert instantly, which i assume is wrong.
how can I solve it ? thanks
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
WemoActivity wemo = new WemoActivity();
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Toast.makeText(context,"in the region" ,Toast.LENGTH_LONG).show();
Log.d(getClass().getSimpleName(), "Entering");
try { wemo.SWITCHON();} catch (Exception e){ //throw new RuntimeException(e);
}
}
else {
Log.d(getClass().getSimpleName(), "Exiting");
Toast.makeText(context,"out of the region" ,Toast.LENGTH_LONG).show();
try { wemo.SWITCHOFF();} catch (Exception e){ //throw new RuntimeException(e);
}
}
sendNotification(context);
}
@SuppressWarnings("deprecation")
public void sendNotification(Context context){
// String extra=arg1.getExtras().getString("alert").toString();
long when = System.currentTimeMillis();
String message = "You are near your office/home.";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,message, when);
String title = "Proximity Alert!";
Intent notificationIntent = new Intent();
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(NOTIFICATION_ID, notification);
return;
}
}
There is some good info on this exact issue here: Android GPS Proximity Alert when starting within the set proximity
I would think a good solution is the simple approach of setting a flag when the user saves the location, and ignore any entering
broadcasts until the device has left the area for the first time.
Something like this:
public class ProximityIntentReceiver extends BroadcastReceiver {
public boolean firstSet = false; //Set this to true when user initially saves location
private static final int NOTIFICATION_ID = 1000;
WemoActivity wemo = new WemoActivity();
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
if (!firstSet){
Toast.makeText(context,"in the region" ,Toast.LENGTH_LONG).show();
Log.d(getClass().getSimpleName(), "Entering");
try { wemo.SWITCHON();} catch (Exception e){ //throw new RuntimeException(e);
}
}
}
else {
if (!firstSet){
Log.d(getClass().getSimpleName(), "Exiting");
Toast.makeText(context,"out of the region" ,Toast.LENGTH_LONG).show();
try { wemo.SWITCHOFF();} catch (Exception e){ //throw new RuntimeException(e);
}
}
else{
firstSet = false;
}
}
sendNotification(context);
}
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.