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

北城余情 提交于 2019-12-13 04:35:57

问题


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);


}}

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/20279876/android-how-to-check-that-whether-i-reach-near-my-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!