gpt4 book ai didi

android - com.google.android.gms.location.LocationListener 的 LocationChanged 未调用

转载 作者:行者123 更新时间:2023-11-29 14:17:00 28 4
gpt4 key购买 nike

我正在创建一个请求 locationUpdate 的服务。但是 LocationChanged 回调仅在服务首次启动时调用,之后不调用。位置更改应在 2km 更改时调用。我做错了吗

       package com.example.qwerty;
import com.google.android.gms.common.ConnectionResult;

import com.google.android.gms.common.api.GoogleApiClient;

import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;

import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;

import com.google.android.gms.location.LocationRequest;

import com.google.android.gms.location.LocationServices;

import com.google.android.gms.location.LocationListener;




import android.os.IBinder;


//Location Callback
public class LocationService extends Service implements ConnectionCallbacks,OnConnectionFailedListener {

private class locationListener implements LocationListener {

public void addLocation(String loc){
SharedPreferences pref= getSharedPreferences(Contant.MYPREFERENCE, MODE_PRIVATE);
Editor edit = pref.edit();
String s ="";
if(pref.contains("location")){
s = pref.getString("location", "");
}
s = s + loc;
edit.putString("location",s).commit();
}

public void getAddress( final double latitude , final double longitude){
new AsyncTask<Double, Void, String>() {
@Override
protected String doInBackground(Double...params){
Geocoder geocoder = new Geocoder(LocationService.this,Locale.getDefault());
String result ="";
List<Address> addressList =null;
try {
addressList = geocoder.getFromLocation(
params[0], params[1], 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
result = sb.toString();
}
}
catch (Exception e) {
result =e.toString();
result = result + " " + latitude + " "+ longitude;
}

return result;
}
@Override
protected void onPostExecute(String result) {
addLocation(result);
}
}.execute(latitude , longitude);
}
@Override
public void onLocationChanged(Location location) {

if(NetworkState.isNetConnected(LocationService.this)) {
getAddress(location.getLatitude(), location.getLongitude());
}else{
addLocation(" lat " + location.getLatitude()+ " long " + location.getLongitude());
}
}

}


GoogleApiClient client = null;
public LocationService() {

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences pref= getSharedPreferences(Contant.MYPREFERENCE, MODE_PRIVATE);
client = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
client.connect();


return START_STICKY;


}
@Override
public void onDestroy() {


}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onConnected(Bundle arg0) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(420000);
mLocationRequest.setSmallestDisplacement(1000);

LocationServices.FusedLocationApi.requestLocationUpdates(client, mLocationRequest , new locationListener());

}

@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {


}
}

最佳答案

我有另一个适合我的解决方案。可能在不久的将来对其他人有帮助。

  1. 设置闹钟n分钟

  2. 在此警报上设置一个广播。当您收到广播时,启动一个服务,该服务将获取当前位置。

  3. 获取位置后停止该服务。

  4. 同样的事情不断发生。

关于android - com.google.android.gms.location.LocationListener 的 LocationChanged 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32745758/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com