Issue
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?
Solution
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 longitude in every 1 min.
public Location getLocation() {
Location location = null;
try {
LocationManager locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// Getting GPS status
boolean 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. See below for a sample
public final class LocationTracker extends Service implements LocationListener {
// Your code here
}
Answered By - keerthana murugesan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.