Android: How to check that whether i reach/near my

2019-08-27 20:26发布

I am developing an application that wakeup to you when you reach/near your destination and send you notification like Alarm.

Is it any way whether i check continuously user's current location and when user is near/reach destination?

If it is possible than please give me sample/tutorial so that i can implement it easily.

Till now,i implement below code that indicates "Notification".

public class ProximityReceiver extends BroadcastReceiver {

private static final int ALARM_DURATION = 6000;
public static final String INTENT_NAME = "com.xxx.xxx.intent.PROXIMITY";
private static final long VIBRATION_PATTERN[];

static {
    long al[] = new long[6];
    al[1] = 1000L;
    al[2] = 500L;
    al[3] = 1000L;
    al[4] = 500L;
    al[5] = 1000L;
    VIBRATION_PATTERN = al;
}

@Override
public void onReceive(Context context, Intent intent) {



    MyMainApplication wakeappapplication = (MyMainApplication) context
            .getApplicationContext();

    if (wakeappapplication.getStatus().equals(WakeAppStatus.STARTED)) {
        wakeappapplication.setStatus(WakeAppStatus.APPROACHING);
        long l = 6000L + System.currentTimeMillis();
        ((AlarmManager) context.getSystemService("alarm")).setRepeating(0,
                l, 6000L, wakeappapplication.getProximityPendingIntent());
    }

    NotificationManager notificationmanager = (NotificationManager) context
            .getSystemService("notification");

    notificationmanager.cancel("ProximityReceiver", 0);

    Notification notification = new Notification(
            R.drawable.ic_stat_notify_wakeapp,
            context.getString(R.string.text_wake_title),
            System.currentTimeMillis());

    PendingIntent pendingintent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainWakeMeUpActivity.class), 0);

    notification.setLatestEventInfo(context,
            context.getString(R.string.text_wake_title),
            context.getString(R.string.text_wake_descr), pendingintent);

    android.net.Uri uri = wakeappapplication.getChosenRingtone();

    if (uri != null)
        notification.sound = uri;

    notification.vibrate = VIBRATION_PATTERN;

    if (wakeappapplication.isRaiseVolume())
        ((AudioManager) context.getSystemService("audio")).adjustVolume(1,
                0);

    notificationmanager.notify("ProximityReceiver", 0, notification);


}}

2条回答
戒情不戒烟
2楼-- · 2019-08-27 21:05

You'll need to implement LocationListener to get the location updates but mind you it can drain the device's battery like crazy. When you've got the current location, you can use distanceTo() method using the obtained location instance to calculate the distance from the destination provided you have the destination lat-long values. Now here you can check whether the value returned by distanceTo() method is within the acceptable range of being close to the destination for example.. acceptable range for me to reach the destination would be if the ditance between current location and destination is within 40 mtrs.

查看更多
The star\"
3楼-- · 2019-08-27 21:16

You should create a geofence and add it to google location services it will notify you (callback) when a user enters in a particular geofence or exits it.

Follow this: http://developer.android.com/training/location/geofencing.html

查看更多
登录 后发表回答