Android LocationListner。。。感觉被吸了:(
有没有人能找到一个有效的解决方案来调用LocationListener的onLocationChanged(…)方法?看起来很多人都面临着这个问题。 即使尝试了很多方法,我的实现也根本没有被调用Android LocationListner。。。感觉被吸了:(,android,android-location,locationlistener,Android,Android Location,Locationlistener,有没有人能找到一个有效的解决方案来调用LocationListener的onLocationChanged(…)方法?看起来很多人都面临着这个问题。 即使尝试了很多方法,我的实现也根本没有被调用 public class MyLocationListener implements LocationListener{ final String _logTag = "Location listener"; public void onLocationChanged(Location
public class MyLocationListener implements LocationListener{
final String _logTag = "Location listener";
public void onLocationChanged(Location location) {
String provider = location.getProvider();
double lat = location.getLatitude();
double lng = location.getLongitude();
float accuracy = location.getAccuracy();
long time = location.getTime();
Log.d("long"," "+lng);
String logMessage = LogHelper.formatLocationInfo(provider, lat, lng, accuracy, time);
Log.d(_logTag, "Monitor Location:" + logMessage);
}
public void onStatusChanged(String s, int i, Bundle bundle) {
String statusMessage = LogHelper.translateStatus(i);
// Log.d(_logTag, "Monitor Location - Status:" + statusMessage);
Set<String> keys = bundle.keySet();
for(String key:keys){
Log.d(_logTag, "Monitor Location - Bundle Key:" + key);
}
}
public void onProviderEnabled(String s) {
Log.d(_logTag, "Monitor Location - Provider ENABLED by USER: " + s);
}
public void onProviderDisabled(String s) {
Log.d(_logTag, "Monitor Location - Provider DISABLED by USER: " + s);
}
}
公共类MyLocationListener实现LocationListener{
最后一个字符串_logTag=“Location listener”;
已更改位置上的公共无效(位置){
字符串提供程序=location.getProvider();
双纬度=location.getLatitude();
double lng=location.getLongitude();
浮点精度=location.getAccurance();
long time=location.getTime();
对数d(“长”和“+lng”);
字符串logMessage=LogHelper.formatLocationInfo(提供者、lat、lng、精度、时间);
Log.d(_logTag,“监视器位置:”+logMessage);
}
状态已更改的公共void(字符串s、int i、Bundle){
String statusMessage=LogHelper.translateStatus(i);
//Log.d(_logTag,“监视器位置-状态:+statusMessage”);
Set keys=bundle.keySet();
用于(字符串键:键){
d(_logTag,“监视器位置-捆绑密钥:”+密钥);
}
}
已提供已启用的公共void(字符串s){
d(_logTag,“监视器位置-由用户启用的提供程序:”+s);
}
公共无效onProviderDisabled(字符串s){
Log.d(_logTag,“监视器位置-由用户禁用的提供程序:”+s);
}
}
这是我过去使用过的,对我来说效果很好:
public class LocationService
extends Service implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private LocationReceiver locationReceiver = new LocationReceiver();
public LocationClient locationClient;
public LocationRequest locationRequest;
private Location previous;
String TAG = "LocationService";
private boolean monitorLocation;
private boolean googleServicesAvailable = false;
@Override
public void onCreate() {
super.onCreate();
this.monitorLocation = false;
//this.locationListener = new LocationListener();
this.locationRequest = LocationRequest.create();
this.locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); // Use high accuracy
this.locationRequest.setInterval(Singletons.Location.regularIntervalTime); // Setting the update interval to 5mins
this.locationRequest.setFastestInterval(Singletons.Location.fastIntervalTime); // Set the fastest update interval to 1 min
this.googleServicesAvailable = servicesConnected();
this.locationClient = new LocationClient(this, this, this);
}
public boolean servicesConnected() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
//Log.d(TAG, "LocationService: Location services available");
return true;
} else {
Log.e(TAG, "Location services not available");
return false;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (!this.googleServicesAvailable || this.locationClient.isConnected() || this.monitorLocation) {
//Log.d(TAG, "LocationService: services already started");
return START_STICKY;
}
setUpLocationClientIfNeeded();
if (!this.locationClient.isConnected() || !this.locationClient.isConnecting() && !this.monitorLocation) {
//Log.d(TAG, "LocationService: service is starting now");
this.monitorLocation = true;
this.locationClient.connect();
}
return Service.START_STICKY;
}
public void setUpLocationClientIfNeeded() {
if (this.locationClient == null) {
this.locationClient = new LocationClient(this, this, this);
}
}
@Override
public void onLocationChanged(Location location) {
if (this.previous == null) {
this.previous = location;
}
if (!this.isSameLocation(location, this.previous)) {
LocationDataSource datasource = new LocationDataSource(getApplicationContext());
datasource.open();
datasource.insertNewLocation(location, this.previous);
datasource.close();
this.previous = location;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(Bundle bundle) {
this.locationClient.requestLocationUpdates(this.locationRequest, this);
}
@Override
public void onDisconnected() {
this.monitorLocation = false;
this.locationClient = null;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
this.monitorLocation = false;
if (connectionResult.hasResolution()) {
Log.d(TAG, "LocationService: We can resolve the problem?");
// If no resolution is available, display an error dialog
} else {
Log.e(TAG, "LocationService: We cannot resolve the gms location problem");
}
}
@Override
public void onDestroy() {
// Turn off the request flag
this.monitorLocation = false;
if (this.googleServicesAvailable && this.locationClient != null) {
//this.locationClient.removeLocationUpdates(this);
// Destroy the current location client
this.locationClient = null;
}
super.onDestroy();
}
public boolean isSameLocation(Location l, Location p) {
return (p.getLongitude() == l.getLongitude()) && (p.getLatitude() == l.getLatitude());
}
}
你为什么不把标题改成“感觉更好”呢?呵呵,等我完成任务后,当然可以@danny117。我还没试过……但确实试过很多次:)@lilott8