我的代码使用gps坐标来定位和保存位置。
当该人进入该区域时,应该创建通知并发出警报。
问题: 当用户保存位置时,它会立即发送警报,我认为这是错误的。
我该如何解决?谢谢
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;
}
}
答案 0 :(得分:1)
这里有一些关于这个问题的好信息:Android GPS Proximity Alert when starting within the set proximity
我认为一个好的解决方案是在用户保存位置时设置标志的简单方法,并忽略任何entering
广播,直到设备第一次离开该区域。
这样的事情:
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);
}