我已经坚持了一个多星期了,我似乎无法弄清楚我做错了究竟是什么。我已经阅读了以下问题,其中任何一个似乎都不适合我:
Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet
google api client callback is never called
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
我尝试做的是将LocationServices API与Geofence类结合使用来检查用户是否在指定区域。问题似乎在于与Google API客户端和/或Locationservices API进行通信。
我在清单中声明了以下内容:
<service android:name=".GeofenceTransitionsIntentService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="value of my key" />
</application>
我在项目中声明了两个与GoogleApiClient交互的Java方法:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void startApiClient(){
if (mGoogleApiClient.isConnected()) {
Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
return;
//Of course the Toast is supposed to fire of when the user cannot
//connect to the google api. However when I make this code to run
//when the user cannot connect to google play services it just shows
//the toast and does not report me the error which is why I in this
//case made this code to run when the user is connected to google play services
}
try {
LocationServices.GeofencingApi.addGeofences
(mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult(). This is also the line where the app seems to crash because it is unable to connect to the google api client
} catch (SecurityException securityException) {
securityException.notify();
}
}
然后我在onCreate()方法中调用buildGoogleApiClient()并在onStart()方法中调用startApiClient():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
…
buildGoogleApiClient();
}
_
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
startApiClient();
}
如果需要,onResult()方法:
public void onResult(Status status) {
if (status.isSuccess()) {
// Update state and save in shared preferences.
mGeofencesAdded = !mGeofencesAdded;
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded);
editor.apply();
}
关于这一点的奇怪之处在于连接实际上已经实例化了一段时间。当我告诉程序在用户未连接时显示Toast时,我的Android监视器告诉我已建立连接但Toast仍然被触发。但是,当我告诉程序在用户连接到Google API客户端时显示Toast时,应用程序崩溃并告诉我Google API客户端尚未连接。
提前感谢您抽出时间帮助我解决这个问题。如果我监督一些非常明显的事情,请原谅我。
答案 0 :(得分:0)
请尝试浏览AndroidHive中的Google客户端位置教程 - Android Location API using Google Play Services。
如给定教程中所述,您需要在活动中进行以下更改以获取用户的当前位置。
- 中的
首先通过
checkPlayServices()
onResume()
来查看Google Play服务的可用性在设备上提供播放服务后,请通过调用
buildGoogleApiClient()
方法构建GoogleApiClient。通过
mGoogleApiClient.connect()
方法调用onStart()
连接到google api客户端。通过调用此方式,系统会根据连接状态触发onConnectionFailed()
,onConnected()
和onConnectionSuspended()
。- 醇>
成功连接google api后,应
displayLocation()
方法调用onConnected()
以获取当前位置。
此外,您还可以检查以下可能导致警报无法正常工作的原因,如Troubleshoot the Geofence Entrance Event中所述。这些是:
重要信息和示例代码可在Creating and Monitoring Geofences和给定教程中找到。