Java 无法连接到Google API客户端?
我已经被困在这个问题上一个多星期了,我似乎不知道我到底做错了什么。我已经阅读了以下问题,但似乎没有一个适合我: 我试图做的是结合使用LocationServices API和Geofence类来检查用户是否在指定区域。问题似乎在于与Google API客户端和/或Locationservices API的通信 我已在我的舱单中声明如下:Java 无法连接到Google API客户端?,java,android,google-play-services,google-api-client,Java,Android,Google Play Services,Google Api Client,我已经被困在这个问题上一个多星期了,我似乎不知道我到底做错了什么。我已经阅读了以下问题,但似乎没有一个适合我: 我试图做的是结合使用LocationServices API和Geofence类来检查用户是否在指定区域。问题似乎在于与Google API客户端和/或Locationservices API的通信 我已在我的舱单中声明如下: <service android:name=".GeofenceTransitionsIntentService" /> <
<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>
然后,我在onCreate()方法中调用BuildGoogleAppClient(),并在onStart()方法中调用StartAppClient():
_
如有必要,onResult()方法:
奇怪的是,一个连接确实会被实例化一段时间。当我告诉程序在用户未连接时显示Toast时,我的Android监视器告诉我已经建立了连接,但Toast仍然会被触发。但是,当我告诉程序在用户连接到GoogleAPI客户端时显示Toast时,应用程序崩溃,并告诉我GoogleAPI客户端尚未连接
提前感谢您抽出一天的时间来帮助我。如果我正在监督一些非常明显的事情,请原谅我。请尝试阅读AndroidHive中谷歌客户端位置的教程- 正如在给定的教程中所讨论的,您需要在活动中进行以下更改以获取用户的当前位置
onResume()中的checkPlayServices()
检查Google Play服务的可用性
buildGoogleAppClient()
方法构建GoogleAppClient
onStart()
方法中的mGoogleApiClient.Connect()
连接到google api客户端。通过调用此函数,onConnectionFailed()
,onConnected()
和onConnectionSuspended()
将根据连接状态触发
onConnected()
方法中调用displayLocation()
,以获取当前位置
- 您的地理围栏内没有准确的位置,或者您的地理围栏太小
- 设备上的Wi-Fi已关闭。确保设备上已启用wifi和位置
- 地理围栏内没有可靠的网络连接
- 警报可能会延迟
重要信息和示例代码可以在给定的教程中找到。请尝试阅读AndroidHive-中有关Google客户端位置的教程 正如在给定的教程中所讨论的,您需要在活动中进行以下更改以获取用户的当前位置
onResume()中的checkPlayServices()
检查Google Play服务的可用性
buildGoogleAppClient()
方法构建GoogleAppClient
onStart()
方法中的mGoogleApiClient.Connect()
连接到google api客户端。通过调用此函数,onConnectionFailed()
,onConnected()
和onConnectionSuspended()
将根据连接状态触发
onConnected()
方法中调用displayLocation()
,以获取当前位置
- 您的地理围栏内没有准确的位置,或者您的地理围栏太小
- 设备上的Wi-Fi已关闭。确保设备上已启用wifi和位置
- 地理围栏内没有可靠的网络连接
- 警报可能会延迟
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();
}
}
@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();
}
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();
}