Get GPS navigation location in a fixed interval of

2019-09-04 19:20发布

问题:

Okay here it goes my app is sort of a GPS tracker app you can say.

If the user is travelling from source to destination, i want to get its GPS location say after 5-7 minutes until the user has reached the destination and also keep send text messages of that position to a specified number.

So my question how can i get user's location and send message after he has closed the application?

The user will fill in the source and destination in my activity and then click a button to send text message and the application closes.

I guess this can be solved with using service class of android...But i really didn't understood how can i use it?

回答1:

Use requestLocationUpdates() of LocationManager class for getting GPS location in certain time interval. Look into this for your reference.

See the following code snippet which would fetch user's current location in latitude and longitutude in every 1 min.

public Location getLocation() { 
    try { 
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
             // getting GPS status 
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
        // if GPS Enabled get lat/long using GPS Services 
        if (isGPSEnabled) {
            if (location == null) { 
                locationManager.requestLocationUpdates( 
                LocationManager.GPS_PROVIDER, 
                MIN_TIME_BW_UPDATES, 
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

                    if (locationManager != null) { 
                    location = locationManager 
                    .getLastKnownLocation (LocationManager.GPS_PROVIDER); 
                    if (location != null) { 
                    latitude = location.getLatitude(); 
                    longitude = location.getLongitude(); 
                  } 
                } 
        } 
    } 
} catch (Exception e) { 
        e.printStackTrace(); 
        } 
        return location; 
    } 

After getting the the Latitude and Longitude use Geocoder to get detailed address.

public List<Address> getCurrentAddress(){
    List<Address> addresses = new ArrayList<Address>();
    Geocoder gcd = new Geocoder(mContext,Locale.getDefault());
    try {
        addresses = gcd.getFromLocation(latitude, longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return addresses;
}

EDIT:

Do all the above functionalitie in a class that extends Service so that it tracks user location eventhough the appln is closed.c below for sample,

public class LocationTracker extends Service implements LocationListener{
     \\your code here
 }