【问题标题】:Android: How to resolve Google API connection fail from a Service?Android:如何解决服务中的 Google API 连接失败?
【发布时间】:2015-09-27 23:52:33
【问题描述】:

here是官方指南提供的代码,这是一个sn-p导致的问题。

@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;
                    }
                });
    }
}

如果我在服务中使用它,当您读取作为参数传递给这些函数的变量 this 时,它们需要一个 Activity 类型。 我应该怎么做?这是一项服务。

出于同样的原因,我无法获得活动结果

@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();
        }
    }
}
}

【问题讨论】:

  • 所以你的问题是获取一个 Activity 的引用??在服务的生命周期中,活动是否还活着?
  • 什么类型的服务:启动、绑定、意图?
  • 在活动可以处于任何状态时开始

标签: android arguments android-service google-play-services


【解决方案1】:

此答案假定您的服务是“已启动”服务。如果是绑定服务或意图服务,请在评论中注明,我将更新此处包含的描述和代码。

我建议的解决方案是实现如下所示的活动来处理分辨率 UI。用此代码替换服务中的onConnectionFailed() 方法,将解析处理交给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)传递给活动。 Activity 处理解析 UI,完成后,将状态作为额外的意图返回给服务。您将需要修改服务的onStartCommand() 中的代码,以检查意图中的额外内容,以确定是第一次调用它来启动服务,还是从ResolverActivity 接收解析状态。

对这种方法的改进是使用PendingIntentResolverActivity 发布通知,而不是立即启动活动。这将为用户提供延迟解决连接故障的选项。

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");
        }
    }
}

【讨论】:

  • 它可以工作,但我添加了 mResolving 布尔值,正如指南所说,以避免重复失败,同时仍然解决以前的问题。
  • 在对话框关闭时也返回 FAILED
  • onActivityResult 在我的情况下总是返回错误的 requestCode 你能在这方面提供帮助吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
相关资源
最近更新 更多