I want to 开发者_运维问答implement whenever I launch my android application,that time I want to start gps,
Whenever I close application, I want to stop gps in device. How to implement this concept in my application ?
The most important thing is to remove the LocationListener in all onPause methods of your Activities. You then can restart the listening in your onResume methods.
If you don't remove the listener in the onPause method the GPS will be active even if your app is the paused in the background. See the activity lifecycle for more information.
John, you need to use the LocationManager class. Here is a tutorial on how to get the users location.
First add bellow Class and interface to your application.
public class GPSManager
{
private static final int gpsMinTime = 500;
private static final int gpsMinDistance = 0;
private LocationManager locationManager = null;
private LocationListener locationListener = null;
private GPSCallback gpsCallback = null;
public GPSManager()
{
locationListener = new LocationListener()
{
public void onProviderDisabled(final String provider)
{
}
public void onProviderEnabled(final String provider)
{
}
public void onStatusChanged(final String provider, final int status, final Bundle extras)
{
}
public void onLocationChanged(final Location location)
{
if (location != null && gpsCallback != null)
{
Log.e("if location "+location,"if gpscallback"+gpsCallback);
gpsCallback.onGPSUpdate(location);
}
else{
Log.e("else location "+location,"else gpscallback"+gpsCallback);
}
}
};
}
public void startListening(final Activity activity)
{
if (locationManager == null)
{
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final String bestProvider = locationManager.getBestProvider(criteria, true);
if (bestProvider != null && bestProvider.length() > 0)
{
locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,
GPSManager.gpsMinDistance, locationListener);
}
else
{
final List<String> providers = locationManager.getProviders(true);
for (final String provider : providers)
{
locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
GPSManager.gpsMinDistance, locationListener);
}
}
}
public void stopListening()
{
try
{
if (locationManager != null && locationListener != null)
{
locationManager.removeUpdates(locationListener);
}
locationManager = null;
}
catch (final Exception ex)
{
}
}
public void setGPSCallback(final GPSCallback gpsCallback)
{
this.gpsCallback = gpsCallback;
}
public GPSCallback getGPSCallback()
{
return gpsCallback;
}
}
Interface:
public interface GPSCallback
{
public abstract void onGPSUpdate(Location location);
}
then implement this interface in your activity and also add below code in your activity
gpsmanager=new GPSManager();
gpsmanager.startListening(YourActicvityname.this);
gpsmanager.setGPSCallback(this);
To stop GPS call gpsmanager.stopListening() at where you want to close the application
精彩评论