gpt4 book ai didi

android - Google FusedLocationApi API 调用 onConnectionSuspended 的时间太多,我无法获取 GPS 位置

转载 作者:行者123 更新时间:2023-11-30 00:42:17 26 4
gpt4 key购买 nike

我使用 GoogleApiClient 获取 GPS 位置。我已经创建了一项服务,每 10 秒从 GPS 获取数据,但问题是调用了很多次 onConnectionSuspended() 而我没有获取任何 GPS 数据。以下是我获取 GPS 位置的服务代码

 <service
android:name="com.GPSService"
android:enabled="true"
android:exported="false"
android:process=":ServiceProcess"
android:stopWithTask="false" />

服务代码

public class GPSService extends Service implements LocationListener, ConnectionCallbacks, OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

protected GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
protected boolean requestingLocationUpdate = true;
private Location mLastLocation;

// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FASTEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 5; // 5 meters

private static final String LOCATION = "Location Not Available";

private double latitude;
private double longitude;
private String mLastUpdateTime;
Intent intents;
RequestParams requestParams = null;

private static final String TAG = "GPSService";


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

@Override
public void onCreate() {
super.onCreate();

helper.insertServiceLog("oncreate");
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d("GPSService", "onCreate");
//

mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mLocationRequest = new LocationRequest();

mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onDestroy() {
Log.i("MyReceiver", "onTaskRemoved called Oooooooooooooppppssssss!!!!");

mGoogleApiClient.disconnect();

}

@Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);

Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

Log.d("GPSService", "onStart");
mGoogleApiClient.connect();

if (mGoogleApiClient.isConnected() && requestingLocationUpdate) {
startLocationUpdates();
}
}
@Override
public final int onStartCommand(Intent intent, int flags, int startId) {

return START_STICKY;
}
@Override
public void onLocationChanged(Location loc) {
//String imei = "2";

mLastLocation = loc;
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

// TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
if (requestingLocationUpdate) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
//LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
startLocationUpdates();
Toast.makeText(this, "Started the location update", Toast.LENGTH_LONG).show();

}
}

@Override
public void onConnectionSuspended(int arg0) {
Log.d("GPSService", "onConnectionSuspended called");
}

/**
* Starting the location updates
*/

protected void startLocationUpdates() {

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

@Override
public void onTaskRemoved(Intent rootIntent) {
Log.i("MyReceiver", "onTaskRemoved called Oooooooooooooppppssssss!!!!");
super.onTaskRemoved(rootIntent);
}
}

谁能帮我解决这个问题。

最佳答案

在不同的进程中运行服务是否有特殊原因?我想这可能与此有关。尝试将服务声明更改为

<service
android:name="com.GPSService"
android:enabled="true"
android:exported="false" />

关于android - Google FusedLocationApi API 调用 onConnectionSuspended 的时间太多,我无法获取 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42366977/

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