谷歌API客户端BUG,发送地点 [英] Google API client bug, sending locations
问题描述
我要创造我送每隔几秒钟消息给GCM。但一段时间后,我想再次删除locationupdates。对于数据发送到GCM的intentservice我使用的PendingIntent。现在每次和它发生了很多我得到这个错误:
I want to create that I send every few seconds a message to GCM. But after some time I want to remove the locationupdates again. For sending the data to the intentservice of gcm I use a pendingintent. Now every time and it happens a lot I get this error :
产生的原因:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.common.internal.n.a(Unknown Source)
at com.google.android.gms.common.api.b.b(Unknown Source)
at com.google.android.gms.internal.lt.removeLocationUpdates(Unknown Source)
at com.example.task_1.Location.LocationUpdate.stopLocationUpdates(LocationUpdate.java:83)
at com.example.task_1.Location.LocationUpdate.onStartCommand(LocationUpdate.java:52)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704)
at android.app.ActivityThread.access$1900(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是我的code:
public class LocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "DRIVER";
private SharedPreferences pref;
private String driverId;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Intent mGcmIntentService;
private PendingIntent mPendingIntent;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
boolean stopService = false;
if (intent != null)
stopService = intent.getBooleanExtra("stopservice", false);
if (stopService)
stopLocationUpdates();
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
pref = getSharedPreferences("driver_app", MODE_PRIVATE);
driverId = pref.getString("driver_id", "");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
public void stopLocationUpdates() {
if(!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
}
mGcmIntentService = new Intent(this,SendDataIntentService.class);
mGcmIntentService.putExtra("ID", "FusedLocation");
mPendingIntent = PendingIntent.getService(this, 0, mGcmIntentService, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mPendingIntent);
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(30000);
startLocationUpdates();
}
private void startLocationUpdates() {
mGcmIntentService = new Intent(this,SendDataIntentService.class);
mGcmIntentService.putExtra("ID", "FusedLocation");
mPendingIntent = PendingIntent.getService(this, 0, mGcmIntentService, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mPendingIntent);
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
任何人知道如何解决这个错误?或如何解决呢?我在网上搜索上,但不能发现任何东西。
Anyone know how to solve this bug? Or how to fix it? I searched on the internet but can't find anything.
推荐答案
的问题是,在你的 stopLocationUpdates()
方法,你是不是在等待该API连你打电话之前 removeLocationUpdates()
。
The problem is that in your stopLocationUpdates()
method, you are not waiting for the API to be connected before you call removeLocationUpdates()
.
一个简单的方法来解决,这将是设置一个布尔标志,当你需要删除位置的回调,但API未连接。
One simple way to fix this would be to set a boolean flag when you need to remove location callbacks, but the API is not connected.
添加一个成员变量:
private boolean isRemoving = false;
然后修改逻辑,这样它等待API来注销的位置回调之前连接。
Then modify the logic so that it waits for the API to connect before unregistering for location callbacks.
在 stopLocationUpdates()
方法:
public void stopLocationUpdates() {
if(!mGoogleApiClient.isConnected()){
isRemoving = true; //added
mGoogleApiClient.connect();
}
else {
mGcmIntentService = new Intent(this, SendDataIntentService.class);
mGcmIntentService.putExtra("ID", "FusedLocation");
mPendingIntent = PendingIntent.getService(this, 0, mGcmIntentService, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mPendingIntent);
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
}
}
在 onConnected()
回调:
@Override
public void onConnected(Bundle arg0) {
if (isRemoving){
stopLocationUpdates();
}
else {
// TODO Auto-generated method stub
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(30000);
startLocationUpdates();
}
}
您也想设置标志返回假的 startLocationUpdates()
:
You would also want to set the flag back to false in startLocationUpdates()
:
private void startLocationUpdates() {
isRemoving = false; //added
mGcmIntentService = new Intent(this,SendDataIntentService.class);
mGcmIntentService.putExtra("ID", "FusedLocation");
mPendingIntent = PendingIntent.getService(this, 0, mGcmIntentService, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mPendingIntent);
}
另外在的onCreate()
:
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
isRemoving = false; //added
pref = getSharedPreferences("driver_app", MODE_PRIVATE);
driverId = pref.getString("driver_id", "");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
}
编辑:要重新启动的位置回调后,他们已经previously取消,您可以使用 onStartCommand()
法
修改 onStartCommand()
,使其既可以停止和启动位置更新:
Modify onStartCommand()
so that it can both stop and start location updates:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
boolean stopService = false;
if (intent != null) {
stopService = intent.getBooleanExtra("stopservice", false);
}
if (stopService) {
stopLocationUpdates();
}
else{
isRemoving = false;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
}
return START_STICKY;
}
然后,为了重新启动位置更新,你会叫 startService()
与有意向 stopservice
设置为false:
Then, in order to re-start location updates, you would call startService()
with an Intent that has stopservice
set to false:
Intent i = new Intent(this, LocationUpdate.class);
i.putExtra("stopservice", false);
startService(i);
这篇关于谷歌API客户端BUG,发送地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!