Java 为什么我的应用程序抛出非法状态异常GoogleAppClient尚未连接?
这是来自我的服务(GetInformationService.onConnect)。奇怪的是,它怎么说我的onConnected方法中没有连接?我见过很多这样的问题,但我仍然无法理解,因为所有的答案都是关于onCreate方法的,但我没有onCreate,因为这是一个服务。以下是我的相关代码(请记住,我正在扩展IntentService):Java 为什么我的应用程序抛出非法状态异常GoogleAppClient尚未连接?,java,android,google-api,google-api-client,Java,Android,Google Api,Google Api Client,这是来自我的服务(GetInformationService.onConnect)。奇怪的是,它怎么说我的onConnected方法中没有连接?我见过很多这样的问题,但我仍然无法理解,因为所有的答案都是关于onCreate方法的,但我没有onCreate,因为这是一个服务。以下是我的相关代码(请记住,我正在扩展IntentService): 尝试在您的服务中包含onCreate(),然后从此处构建GoogaPicClient @Override protected void onHandleIn
尝试在您的服务中包含
onCreate()
,然后从此处构建GoogaPicClient
@Override
protected void onHandleIntent(Intent intent)
{
g = Globals.getInstance();
context = this;
locationProvider = LocationServices.FusedLocationApi;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationRequest = new LocationRequest();
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(15000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
googleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
尝试在您的服务中包含
onCreate()
,然后从此处构建GoogaPicClient
@Override
protected void onHandleIntent(Intent intent)
{
g = Globals.getInstance();
context = this;
locationProvider = LocationServices.FusedLocationApi;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationRequest = new LocationRequest();
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(15000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
googleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
您的服务类中仍然可以有一个onCreate。您的服务类中仍然可以有一个onCreate。我是否应该在OnHandleContent的原始帖子中保留其他代码?还是全部移动?其他代码可以。只有初始化应该在onCreate()中完成,我应该在OnHandleContent的原始帖子中保留其他代码吗?还是全部移动?其他代码可以。只应在onCreate()中完成初始化
@Override
public void onCreate() {
super.onCreate();
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
}