我已经使用了它并且工作正常:获取服务位置
这里的服务等级:
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// start location manager
LocationDetermined objLocationDetemined = new LocationDetermined(this);
objLocationDetemined.getlocation(this);
objLocationDetemined.getRecentKnownLocation(this);
}
位置类在这里:
public class LocationDetermined implements OnInitListener {
String TAG = "LocationDeterminedClass";
private static LocationManager locationManager;
double Lat = 0.0;
double Long = 0.0;
GeoPoint geoPoint;
private Toast mToast;
Context appContext;
static MyLocationListener locationListener;
public LocationDetermined(Context context) {
this.appContext = context;
}
public MyLocationListener getlocation(Context appContext) {
locationManager = (LocationManager) appContext
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(criteria, true);
Log.d(TAG, "------provider------" + provider);
if (provider != null && locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 0, 0,
locationListener);
} else {
Toast.makeText(appContext, "No provider available to get loctaion",
2000).show();
}
return locationListener;
}
private class MyLocationListener implements LocationListener {
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onLocationChanged(final Location argLocation) {
// TODO Auto-generated method stub
if (argLocation != null) {
Lat = argLocation.getLatitude();
Long = argLocation.getLongitude();
Log.e("OnLocationChanged:", "lat:" + Lat + " long:" + Long);
Log.d("service gps", "lat" + argLocation.getLatitude() + "-"
+ argLocation.getLongitude());
Constant.mLatitude = argLocation.getLatitude();
Constant.mLongitude = argLocation.getLongitude();
}
}
}
public GeoPoint getRecentKnownLocation(Context appContext) {
Location locGps = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locNet = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location locPassive = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (locGps != null) {
Lat = locGps.getLatitude();
Long = locGps.getLongitude();
Constant.mLatitude = locGps.getLatitude();
Constant.mLongitude = locGps.getLongitude();
geoPoint = new GeoPoint((int) (Lat * 1000000),
(int) (Long * 1000000));
return geoPoint;
} else if (locNet != null) {
Lat = locNet.getLatitude();
Long = locNet.getLongitude();
Constant.mLatitude = locNet.getLatitude();
Constant.mLongitude = locNet.getLongitude();
geoPoint = new GeoPoint((int) (Lat * 1000000),
(int) (Long * 1000000));
return geoPoint;
}
return geoPoint;
}
private void fetchedSavedLocationDetail(Context mContext) {
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
public static void removeLocationListener() {
locationManager.removeUpdates(locationListener);
}
}
我在常量类中使用这些变量来存储位置 public static double mLatitude = 0.0, mLongitude = 0.0;
启动服务 startService(new Intent(this, MyService.class));