使用addProximityAlert在Android上通过接近警报

时间:2018-07-25 00:51:51

标签: android android-location locationmanager android-broadcastreceiver

当用户到达某个地方附近时,我想在我的应用程序中发出警告。

所以我设置了警告的地方

    // 100 meter radius
    float radius = 100f;

    // Expiration is 10 Minutes (10mins * 60secs * 1000milliSecs)
    long expiration = 600000;

    Intent intent = new Intent(PROXIMITY_INTENT_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    locationManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);

每当设备移动时,该方法就称为更新位置。

public static final String PROXIMITY_INTENT_ACTION = "com.mycompanie.receivers.ProximityAlert";
@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Location Device: " + location.getLatitude() + "," + location.getLongitude());
    Intent intent = new Intent(PROXIMITY_INTENT_ACTION);
    sendBroadcast(intent);
}

并且调用了onReceiver方法,但是即使使用模拟器或某些伪造的GPS放置相同的位置,getting_closer变量也始终为假

public class ProximityAlert extends BroadcastReceiver {

public static final String EVENT_ID_INTENT_EXTRA = LocationManager.KEY_PROXIMITY_ENTERING;

   @Override
   public void onReceive(Context context, Intent intent) {
       Boolean getting_closer = intent.getBooleanExtra(EVENT_ID_INTENT_EXTRA, false);
       if (getting_closer)
        Log.d("Radius", "Hey, I just entered your radius!");
       else
        Log.d("Radius", "I just exited your radius!");
   }
}

有人可以帮我吗?

0 个答案:

没有答案