import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSInfoService {
private Context context;
private LocationManager manager;
SharedPreferences sp ;
private GPSInfoService(Context context){
this.context= context;
manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
private static GPSInfoService mGPSService;
public synchronized static GPSInfoService getInstance(Context context){
if(mGPSService==null)
mGPSService = new GPSInfoService(context);
return mGPSService;
}
public void registerLocationUpdates(){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider =manager.getBestProvider(criteria, true);
manager.requestLocationUpdates(provider, 60000, 0, getLinster());
}
public void cancleLocationUpdates(){
manager.removeUpdates(getLinster());
}
private static MyGPSLinster myGPSLinser;
private MyGPSLinster getLinster(){
if(myGPSLinser==null)
myGPSLinser = new MyGPSLinster();
return myGPSLinser;
}
/**
* 获取手机的最后一次位置
* @return
*/
public String getLastPosition(){
return sp.getString("lastlocation", "");
}
private class MyGPSLinster implements LocationListener{
public void onLocationChanged(Location location) {
double latitude= location.getLatitude();
double longitude = location.getLongitude();
String locationstr = "jing du "+ longitude + " weidu :"+latitude;
Editor editor = sp.edit();
editor.putString("lastlocation", locationstr);
editor.commit();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
}
}