繁体   English   中英

在错误的时间触发接近警报

[英]proximity alert firing at wrong time

我的代码使用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;


    }




}

有关此确切问题,这里有一些很好的信息: 在设置的接近范围内启动时,Android GPS接近警报

我认为一个好的解决方案是在用户保存位置时设置标志的简单方法,并忽略任何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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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