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

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

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

因此,当我创建 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 中?这种情况下的最佳做法是什么?

最佳答案

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

希望这对您有所帮助...

问候!

关于Android 在出错时显示来自服务的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19147658/

相关文章:

android - 使用 react-native 的 android 上的空白屏幕(仅限发布)

android - 如何使我的布局像 android 中的动画一样完全移动到屏幕左侧?

Android Toolbar home as up indicator material design 点击效果不显示

安卓 NFC : 'd' prepended on NDEF payload

android - 在 android 中创建一个不可停止的服务

android - 将 Google Play 服务版本添加到您的应用 list ?

java - 更改 navigationView 的单个特定菜单项的背景颜色

android - 启动服务并返回数据

java - 仅在用户移动时运行服务以收集传感器数据

android-gradle-plugin - React-Native Android Build Failure : Execution failed for task ':app:processDebugResources' . 资源处理失败