【问题标题】:Android Show Dialog From a Service on error错误时来自服务的 Android 显示对话框
【发布时间】:2013-10-09 11:17:44
【问题描述】:

我有一个实现GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListenerService

所以,当我创建Service 时,我会检查Google Play Services 是否可用,或者是否是正确的版本:

private boolean servicesConnected() {

  // Check that Google Play services is available
  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

  // If Google Play services is available
  if (ConnectionResult.SUCCESS == resultCode) {
      return true;
  } else {
    // Get the error code
      GooglePlayServicesUtil
          .getErrorDialog(resultCode, this, 0).show();// This won't work in a service
  }

}//end

那么,如果我返回一个错误代码,我该如何响应它(向用户显示带有选项的 Dialog),因为我在 Service 内部?在这种情况下,最佳做法是什么?

【问题讨论】:

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


    【解决方案1】:

    最佳实践是服务仅适用于长任务“非 UI 相关”功能,如果您需要显示不需要服务的消息机会很高,它们应该是 100 % 后台长任务功能,根据谷歌的文档。实际上必须显示错误消息或与 UI 相关的内容将是一个好兆头,可以为您正在做的任何事情寻找不同的方法,无论如何,如果显示消息非常重要,我认为您的活动应该是可见的,您总是可以将服务绑定到您的活动并在其上执行显示对话框的方法...

    希望这个帮助...

    问候!

    【讨论】:

    • 好的。所以你说我应该在开始服务之前进行Google Play Services 检查。对吗?
    • @NicHubbard:正确。
    • 如果这会阻止您将您的服务与某种“UI”反馈耦合,那么肯定...
    • 完美。多谢你们。现在有点明显了。 :)
    【解决方案2】:

    您始终可以在服务中启动活动。

    【讨论】:

    • 这不是糟糕的应用程序设计吗?你的意思是,创建一个Activity 来传递给getErrorDialog() 函数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多