簡體   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