Google 客户端 Api 未连接 activity 恢复
Google Client Api is not connecting on activity resume
我正在使用定位服务获取当前位置 api。实际上,我已经在 onResume() 方法中使用位置管理器 class 验证了位置是否已启用,如果未启用,则使用 intent 将用户发送到位置设置。启用位置后返回 activity 并尝试连接 Google 客户端 api。但它没有连接,也没有给出任何位置。我用过这个代码。
protected synchronized void buildGoogleApiClient(){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(connectionCallbackListener)
.addOnConnectionFailedListener(connectionFailedListener)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Location is disabled")
.setMessage("Please enable your location")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
mGoogleApiClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
GoogleApiClient.ConnectionCallbacks connectionCallbackListener = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null){
Log.v("Latitude", String.valueOf(mLastLocation.getLatitude()));
Log.v("LOngitude", String.valueOf(mLastLocation.getLongitude()));
startIntentService();
}
}
@Override
public void onConnectionSuspended(int i) {
}
};
GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("Connection failed", String.valueOf(connectionResult.getErrorCode()));
}
};
我在 onCreate() 回调中调用 buildGoogleApiClient() 方法。请有人告诉我解决方案,因为它非常重要。
您可以使用 startActivityForResult()
,然后在 onActivityResult()
中检查是否已从对话框提示中启用其中一个位置提供程序。
如果启用了 GPS 或网络位置提供商,则调用 mGoogleApiClient.connect()
。
另请注意,您可以在初始检查中使用 &&
而不是 ||
,因为如果至少有一个位置提供商,则实际上不需要用 "Location is disabled" 提示用户已启用。
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Location is disabled")
.setMessage("Please enable your location")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
mGoogleApiClient.connect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//At least one provider enabled, connect GoogleApiClient
mGoogleApiClient.connect();
}
}
}
我正在使用定位服务获取当前位置 api。实际上,我已经在 onResume() 方法中使用位置管理器 class 验证了位置是否已启用,如果未启用,则使用 intent 将用户发送到位置设置。启用位置后返回 activity 并尝试连接 Google 客户端 api。但它没有连接,也没有给出任何位置。我用过这个代码。
protected synchronized void buildGoogleApiClient(){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(connectionCallbackListener)
.addOnConnectionFailedListener(connectionFailedListener)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Location is disabled")
.setMessage("Please enable your location")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
mGoogleApiClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
GoogleApiClient.ConnectionCallbacks connectionCallbackListener = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null){
Log.v("Latitude", String.valueOf(mLastLocation.getLatitude()));
Log.v("LOngitude", String.valueOf(mLastLocation.getLongitude()));
startIntentService();
}
}
@Override
public void onConnectionSuspended(int i) {
}
};
GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("Connection failed", String.valueOf(connectionResult.getErrorCode()));
}
};
我在 onCreate() 回调中调用 buildGoogleApiClient() 方法。请有人告诉我解决方案,因为它非常重要。
您可以使用 startActivityForResult()
,然后在 onActivityResult()
中检查是否已从对话框提示中启用其中一个位置提供程序。
如果启用了 GPS 或网络位置提供商,则调用 mGoogleApiClient.connect()
。
另请注意,您可以在初始检查中使用 &&
而不是 ||
,因为如果至少有一个位置提供商,则实际上不需要用 "Location is disabled" 提示用户已启用。
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Location is disabled")
.setMessage("Please enable your location")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
mGoogleApiClient.connect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//At least one provider enabled, connect GoogleApiClient
mGoogleApiClient.connect();
}
}
}