gpt4 book ai didi

android - Android服务为什么不调用OnlocationChanged?

转载 作者:太空狗 更新时间:2023-10-29 14:05:10 25 4
gpt4 key购买 nike

我有一项服务可以在后台线程中发送位置更新。我已将 list 中的服务声明为:

<service android:name="mypackage.LocationUpdater"></service>

我从 OnStartComand 中的 Activity 获取数据。但是根本没有调用 OnLocationChanged。我无法从服务中的任何位置调用 sendLocation() 方法。

public class LocationUpdater extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

LocationListener mLocationListener;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation; //this object should be used to request location updates
String p = "test", d = "test";
@Override
public void onCreate() {
buildGoogleApiClient();

}

protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
p = intent.getStringExtra("ptok");
d = intent.getStringExtra("dtok");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_NOT_STICKY;
}


@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);

mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

if (mLastLocation != null) {
String latitude = String.valueOf(mLastLocation.getLatitude());
String longitude = String.valueOf(mLastLocation.getLongitude());
sendLocation(p, latitude, longitude, d);
}
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

if(location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.i("info", "Latitude :: " + latitude);
Log.i("info", "Longitude :: " + longitude);
//sending location details
sendLocation(p, String.valueOf(latitude), String.valueOf(longitude), d);
}
}
}

我还必须添加 onConnected 也永远不会被调用。我所看到的只是 onStartComand 被调用并使用 intent 传递从我的 Activity 发送的值。

最佳答案

文档没有描述这个但是 based on this SO accepted answer ,我必须明确调用 mGoogleApiClient.connect();在我的 onCreate 方法中。您在 onStart() 中调用它;从 Activity 。

关于android - Android服务为什么不调用OnlocationChanged?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000742/

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