Android 如何处理服务中的Google play服务错误
我已经使用以下代码检查了设备上的Google play服务apkAndroid 如何处理服务中的Google play服务错误,android,google-play-services,android-dialogfragment,Android,Google Play Services,Android Dialogfragment,我已经使用以下代码检查了设备上的Google play服务apk 他们分别创建了ErrorDialogFragment类并显示对话框,但在我的代码中,我直接使用了GooglePlayServicesUtil.batherRorDialogFragment方法。为什么他们使用GooglePlayServicesUtil.getErrorDialog方法创建了单独的对话框片段和setDialog 他们检查了活动中的播放服务。但是如何在连接LocationClient以获取位置的服务中进行检查。如果我
ErrorDialogFragment
类并显示对话框,但在我的代码中,我直接使用了GooglePlayServicesUtil.batherRorDialogFragment
方法。为什么他们使用GooglePlayServicesUtil.getErrorDialog
方法创建了单独的对话框片段和setDialogLocationClient
以获取位置的服务中进行检查。如果我在服务中进行检查,如何处理错误,因为没有活动可见,所以无法显示对话框片段
您是否已解决问题?请分享您是如何解决的?您可以发送一条消息(通过Square的Otto或LocalBroadcastManager),然后在接收方(您的活动或片段…)为消息注册一个侦听器。
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener{
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000 ;
private static final String TAG = "MainActivity";
private LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(this,this,this);
}
@Override
protected void onResume() {
super.onResume();
if(playServicesConnected()){
mLocationClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if(playServicesConnected()) {
mLocationClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CONNECTION_FAILURE_RESOLUTION_REQUEST:
switch(resultCode){
case Activity.RESULT_OK:
Log.d(TAG,"Play Connected");
Log.d(TAG,"Resolution Available");
break;
default:
Log.d(TAG,"Play Disconnected");
Log.d(TAG,"No Resolution");
break;
}
default:
Log.d(TAG,"Some other request from Play Services");
break;
}
}
/**
* Check Google Play Service apk available on device
* If not exist return error dialog to install or update google play service apk
* @return boolean It returns true if play service apk available,otherwise return false.
*/
private boolean playServicesConnected()
{
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(ConnectionResult.SUCCESS == resultCode){
Log.d(TAG,"Google Play Services Available");
return true;
}else if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
Log.d(TAG,"Google Play Services Not Available");
GooglePlayServicesUtil.showErrorDialogFragment(resultCode,this,CONNECTION_FAILURE_RESOLUTION_REQUEST);
return false;
}else {
Log.d(TAG,"This device not supported");
//Toast.makeText(this,"This device not supported ->"+ConnectionResult,Toast.LENGTH_SHORT).show();
GooglePlayServicesUtil.showErrorDialogFragment(resultCode,this,CONNECTION_FAILURE_RESOLUTION_REQUEST);
return false;
}
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG,"Location client Connected");
Toast.makeText(this,"Location client Connected",Toast.LENGTH_SHORT).show();
Location mCurrentLocation = mLocationClient.getLastLocation();
Log.d(TAG,"mCurrentLocation ->" + mCurrentLocation);
}
@Override
public void onDisconnected() {
Log.d(TAG,"Location client DisConnected");
Toast.makeText(this,"Location client DisConnected",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG,"Location client connection failed");
Toast.makeText(this,"Location client connection failed",Toast.LENGTH_SHORT).show();
if(connectionResult.hasResolution()){
Log.d(TAG, "Location client connection failed -> Resolution available");
try {
connectionResult.startResolutionForResult(this,CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, e.toString());
Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
}
}
else
{
Log.d(TAG, "Location client connection failed -> No Resolution");
GooglePlayServicesUtil.showErrorDialogFragment(connectionResult.getErrorCode(),this,CONNECTION_FAILURE_RESOLUTION_REQUEST);
}
}
}