定位类型
- 网络定位,根据你的ip地址来划定一个范围
- 基站定位,根据连接的基站来定位
- gps定位,根据卫星来定位
- A-GPS辅助gps定位系统,通过网络和gps共同定位,一般Android手机都采用这种定位方式。
代码实现
package com.itheima52.mobilesafe.service;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
/**
* 获取经纬度坐标的service
*
* @author Kevin
*
*/
public class LocationService extends Service {
private LocationManager lm;
private MyLocationListener listener;
private SharedPreferences mPref;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mPref = getSharedPreferences("config", MODE_PRIVATE);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(criteria, true);
listener = new MyLocationListener();
lm.requestLocationUpdates(bestProvider, 0, 0, listener);
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
System.out.println("get location!");
mPref.edit()
.putString(
"location",
"j:" + location.getLongitude() + "; w:"
+ location.getLatitude()).commit();
stopSelf();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("onStatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
System.out.println("onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
System.out.println("onProviderDisabled");
}
}
@Override
public void onDestroy() {
super.onDestroy();
lm.removeUpdates(listener);
}
}