Android:如何解决服务中的Google API连接失败?

这里是官方指南提供的代码,而这是一个导致问题的片段。

@Override public void onConnectionFailed(ConnectionResult result) { if (mResolvingError) { // Already attempting to resolve an error. return; } else if (result.hasResolution()) { try { mResolvingError = true; result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); } catch (IntentSender.SendIntentException e) { // There was an error with the resolution intent. Try again. mGoogleApiClient.connect(); } } else { // Show dialog using GooglePlayServicesUtil.getErrorDialog() mResolvingError = true; GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, REQUEST_RESOLVE_ERROR) .setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { mResolvingError = false; } }); } } 

如果我在服务中使用它,当你读取这些作为parameter passing给这些函数的variables时,他们期望一个Activitytypes。 我应该怎么做? 这是一个服务。

出于同样的原因,我不能得到活动的结果

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_RESOLVE_ERROR) { mResolvingError = false; if (resultCode == RESULT_OK) { // Make sure the app is not already connected or attempting to connect if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) { mGoogleApiClient.connect(); } } } } 

Solutions Collecting From Web of "Android:如何解决服务中的Google API连接失败?"

这个答案假设你的服务是一个“开始”服务。 如果是绑定服务或意向服务,请在注释中注明,我将更新此处包含的描述和代码。

我build议的解决scheme是实现下面显示的活动来处理分辨率UI。 用下面的代码replace服务中的onConnectionFailed()方法,将parsing处理交给ResolverActivity

 @Override public void onConnectionFailed(ConnectionResult result) { Intent i = new Intent(this, ResolverActivity.class); i.putExtra(ResolverActivity.CONNECT_RESULT_KEY, result); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } 

将下面显示的活动添加到您的应用中。 如果服务中的连接请求失败,则连接结果(可以是Parcelable )将传递给该活动。 该活动处理分辨率UI,并在完成时将状态作为一个额外的意向返回给服务。 您需要修改服务的onStartCommand()的代码,以检查意图中的代码,以确定是第一次启动服务还是从ResolverActivity接收parsing状态。

对此方法的改进是使用PendingIntent for ResolverActivity发布通知,而不是立即启动活动。 这将给用户延迟解决连接失败的选项。

 public class ResolverActivity extends AppCompatActivity { public static final String TAG = "ResolverActivity"; public static final String CONNECT_RESULT_KEY = "connectResult"; public static final String CONN_STATUS_KEY = "connectionStatus"; public static final int CONN_SUCCESS = 1; public static final int CONN_FAILED = 2; public static final int CONN_CANCELLED = 3; // Request code to use when launching the resolution activity private static final int REQUEST_RESOLVE_ERROR = 1111; private static final String ERROR_CODE_KEY = "errorCode"; private static final String DIALOG_FRAG_TAG = "errorDialog"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate()"); // No content needed. //setContentView(R.layout.activity_main); Intent i = getIntent(); ConnectionResult result = i.getParcelableExtra(CONNECT_RESULT_KEY); if (result.hasResolution()) { try { Log.i(TAG, "Starting error resolution..."); result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); } catch (IntentSender.SendIntentException e) { // There was an error with the resolution intent. sendStatusToService(CONN_FAILED); finish(); } } else { // Show dialog using GooglePlayServicesUtil.getErrorDialog() ErrorDialogFragment.newInstance(result.getErrorCode()) .show(getSupportFragmentManager(), DIALOG_FRAG_TAG); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent result) { if (requestCode == REQUEST_RESOLVE_ERROR) { if (resultCode == RESULT_OK) { Log.i(TAG, "onActivityResult(): Connection problem resolved"); sendStatusToService(CONN_SUCCESS); } else { sendStatusToService(CONN_CANCELLED); Log.w(TAG, "onActivityResult(): Resolution cancelled"); } // Nothing more to do in this activity finish(); } } private void sendStatusToService(int status) { Intent i = new Intent(this, MyGoogleApiService.class); i.putExtra(CONN_STATUS_KEY, status); startService(i); } // Fragment to display an error dialog public static class ErrorDialogFragment extends DialogFragment { public static ErrorDialogFragment newInstance(int errorCode) { ErrorDialogFragment f = new ErrorDialogFragment(); // Pass the error that should be displayed Bundle args = new Bundle(); args.putInt(ERROR_CODE_KEY, errorCode); f.setArguments(args); return f; } @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { // Get the error code and retrieve the appropriate dialog int errorCode = getArguments().getInt(ERROR_CODE_KEY); return GooglePlayServicesUtil.getErrorDialog( errorCode, getActivity(), REQUEST_RESOLVE_ERROR); } @Override public void onDismiss(DialogInterface dialog) { Log.i(TAG, "Dialog dismissed"); } } }