import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.ecity.loactionbynetandgps.common.Common;
/**
* @author fjm
*
*/
public class LocationSvc extends Service {
private static final String TAG = "LocationSvc";
private LocationManager locationManager;
private MyLocationListner gpsListener;
private MyLocationListner networkListener;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
gpsListener = new MyLocationListner();
networkListener = new MyLocationListner();
}
@Override
public void onStart(Intent intent, int startId) {
boolean isEnable = false;
if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog builder = new AlertDialog.Builder(getApplicationContext())
.setMessage("Enable Network Connection")
.setTitle("Warning!")
.setCancelable(true)
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
} ).show();
}
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 2000, 0, networkListener);
isEnable = true;
}
if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000 * 2, 50, gpsListener);
isEnable = true;
}
if (!isEnable)
Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}
@Override
public boolean stopService(Intent name) {
locationManager.removeUpdates(gpsListener);
locationManager.removeUpdates(networkListener);
return super.stopService(name);
}
// GPS和移动网络的监听器,回调函数
private class MyLocationListner implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the location provider.
Log.v("GPSTEST",
"Got New Location of provider:" + location.getProvider());
if (currentLocation != null) {
if (isBetterLocation(location, currentLocation)) {
Log.v("GPSTEST", "It's a better location");
currentLocation = location;
showLocation(location);
} else {
Log.v("GPSTEST", "Not very good!");
}
} else {
Log.v("GPSTEST", "It's first location");
currentLocation = location;
showLocation(location);
}
/*// 移除基于LocationManager.NETWORK_PROVIDER的监听器
if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
locationManager.removeUpdates(this);
}*/
// 通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LOCATION, location.toString());
sendBroadcast(intent);
System.out.println(location.toString());
// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。
// locationManager.removeUpdates(this);
// stopSelf();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// 添加移除的网络监听器
if (LocationProvider.OUT_OF_SERVICE == status) {
Log.v("GPSTEST", "GPS服务丢失,切换至网络定位");
locationManager
.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
networkListener);
}
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
private Location currentLocation;
// 打印位置做标信息
private void showLocation(Location location) {
// 纬度
Log.v("GPSTEST", "Latitude:" + location.getLatitude());
// 经度
Log.v("GPSTEST", "Longitude:" + location.getLongitude());
// 精确度
Log.v("GPSTEST", "Accuracy:" + location.getAccuracy());
// Location还有其它属性,请自行探索
System.out.println(location.toString());
}
// 判断网络和GPS定位精度,哪个更高
private static final int CHECK_INTERVAL = 1000 * 30;
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
// 当前定位为空,直接返回
if (currentBestLocation == null) {
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;
boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location,
// use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must
// be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}