//GPS定位不需要在网络条件下可以运行,但是室内没法定位,必须到空旷的街道上(天上卫星与GPS芯片进行交互)
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class GPSService extends Service
{
public static final long REFRESH_LOCATION_TIME_IN_MILL = 1000 * 10;// gps最短获取时间(单位毫秒)
public static final float REFRESH_LOCATION_DISTANCE_IN_METER = 5f;// gps最小获取距离(单位米)
private LocationManager lm;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
LocationListener listener = new LocationListener()
{
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public void onProviderEnabled(String provider)
{
if (LocationManager.GPS_PROVIDER.equals(provider))
{
// gps设备被开启
}
}
@Override
public void onProviderDisabled(String provider)
{
if (LocationManager.GPS_PROVIDER.equals(provider))
{
// gps 关闭
}
}
@Override
public void onLocationChanged(Location location)
{
// 坐标改变
Toast.makeText(
getApplicationContext(),
"经度:" + location.getLongitude() + ",纬度:"
+ location.getLatitude(), Toast.LENGTH_LONG)
.show();
}
};
@Override
public void onCreate()
{
Log.i("GPSService", "gps服务开启了");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
REFRESH_LOCATION_TIME_IN_MILL,
REFRESH_LOCATION_DISTANCE_IN_METER, listener);
}
@Override
public void onDestroy()
{
super.onDestroy();
lm.removeUpdates(listener);
}
}