Android显示错误时来自服务的对话框

Android显示错误时来自服务的对话框,android,android-service,google-play-services,android-location,Android,Android Service,Google Play Services,Android Location,我有一个服务,它实现了GooglePlayServicesClient.ConnectionCallbacks、GooglePlayServicesClient.OnConnectionFailedListener、LocationListener 因此,当我创建服务时,我会检查Google Play Services是否可用,或者版本是否正确: private boolean servicesConnected() { // Check that Google Play services

我有一个
服务
,它实现了
GooglePlayServicesClient.ConnectionCallbacks、GooglePlayServicesClient.OnConnectionFailedListener、LocationListener

因此,当我创建
服务
时,我会检查
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

因此,如果我返回一个错误代码,我如何响应它(向用户显示一个带有选项的
对话框
),因为我在
服务中
?在这种情况下,最佳做法是什么?

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

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

希望这有助于


问候

这不是很糟糕的应用程序设计吗?您的意思是创建一个
活动
传递到
getErrorDialog()
函数中吗?好的。所以你是说我应该在启动服务之前检查一下
googleplayservices
。对吗?@NicHubbard:对。如果这会阻止你将你的服务与某种“UI”反馈结合起来,那绝对……完美。谢谢各位。现在有点明显了。:)