自定义“更新Google Play服务”对话框

时间:2017-06-09 07:43:34

标签: android

我试图根据来自方法

的状态代码自定义显示错误的Dialog
  

googleApiAvailability.isGooglePlayServicesAvailable(活性);

此方法返回一个状态代码,然后将其传递给方法

  

googleApiAvailability.getErrorDialog(activity,status,2404).show();

然后会向用户显示一个对话框,询问他们是否需要根据错误代码更新Google Play服务或启用它们等(本例中为“状态”)。从我可以告诉他们是根据状态设置对话框文本,但我想稍微自定义它因为我用它来通知所以我想在对话框中说用户不能使用通知而没有这个在应用上安装/启用/更新功能?

此外,我希望能够添加一个带有自定义文字的取消按钮,例如“它还可以”或者其他类似的内容?

我在另一个应用程序中看到了类似的东西,并认为这是一个好主意。

编辑:

   public static boolean checkGooglePlayServices(Activity activity) {
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
    if (status != ConnectionResult.SUCCESS) {
      if (googleApiAvailability.isUserResolvableError(status)) {
        Dialog dialog = googleApiAvailability.getErrorDialog(activity, status, 1);
        dialog.setTitle("This is a test title");
        dialog.show();
      }
      return false;
    }
    return true;
  }

2 个答案:

答案 0 :(得分:0)

resultCode可以具有以下值。

public static final int SUCCESS = 0;
public static final int SERVICE_MISSING = 1;
public static final int SERVICE_VERSION_UPDATE_REQUIRED = 2;
public static final int SERVICE_DISABLED = 3;
public static final int SIGN_IN_REQUIRED = 4;
public static final int INVALID_ACCOUNT = 5;
public static final int RESOLUTION_REQUIRED = 6;
public static final int NETWORK_ERROR = 7;
public static final int INTERNAL_ERROR = 8;
public static final int SERVICE_INVALID = 9;
public static final int DEVELOPER_ERROR = 10;
public static final int LICENSE_CHECK_FAILED = 11;
public static final int CANCELED = 13;
public static final int TIMEOUT = 14;
public static final int INTERRUPTED = 15;
public static final int API_UNAVAILABLE = 16;
public static final int SIGN_IN_FAILED = 17;
public static final int SERVICE_UPDATING = 18;
public static final int SERVICE_MISSING_PERMISSION = 19;
public static final int RESTRICTED_PROFILE = 20;

不要调用getErrorDialog而是调用你的owndialog并采取相应的行动..

 static String getStatusString(int var0) {
        switch(var0) {
        case -1:
            return "UNKNOWN";
        case 0:
            return "SUCCESS";
        case 1:
            return "SERVICE_MISSING";
        case 2:
            return "SERVICE_VERSION_UPDATE_REQUIRED";
        case 3:
            return "SERVICE_DISABLED";
        case 4:
            return "SIGN_IN_REQUIRED";
        case 5:
            return "INVALID_ACCOUNT";
        case 6:
            return "RESOLUTION_REQUIRED";
        case 7:
            return "NETWORK_ERROR";
        case 8:
            return "INTERNAL_ERROR";
        case 9:
            return "SERVICE_INVALID";
        case 10:
            return "DEVELOPER_ERROR";
        case 11:
            return "LICENSE_CHECK_FAILED";
        case 13:
            return "CANCELED";
        case 14:
            return "TIMEOUT";
        case 15:
            return "INTERRUPTED";
        case 16:
            return "API_UNAVAILABLE";
        case 17:
            return "SIGN_IN_FAILED";
        case 18:
            return "SERVICE_UPDATING";
        case 19:
            return "SERVICE_MISSING_PERMISSION";
        case 20:
            return "RESTRICTED_PROFILE";
        case 21:
            return "API_VERSION_UPDATE_REQUIRED";
        case 42:
            return "UPDATE_ANDROID_WEAR";
        case 99:
            return "UNFINISHED";
        case 1500:
            return "DRIVE_EXTERNAL_STORAGE_REQUIRED";
        default:
            return (new StringBuilder(31)).append("UNKNOWN_ERROR_CODE(").append(var0).append(")").toString();
        }
    }

PS:有关详细信息,请参阅ConnectionResult.java类。

答案 1 :(得分:0)

警告:应谨慎处理以下“解决方案”,因为它不使用公共 API(据我所知),并且字符串名称将来可能会更改。

实现自定义对话框(如 lelloman 建议的那样)可能是更好的解决方案。

我在 play-services-base 资源文件夹 res/values/values.xml 中发现了以下字符串,这些字符串用于错误对话框(取决于错误代码):

  • common_google_play_services_enable_button
  • common_google_play_services_enable_text
  • common_google_play_services_enable_title
  • common_google_play_services_install_button
  • common_google_play_services_install_text
  • common_google_play_services_install_title
  • common_google_play_services_notification_channel_name
  • common_google_play_services_notification_ticker
  • common_google_play_services_unsupported_text
  • common_google_play_services_update_button
  • common_google_play_services_update_text
  • common_google_play_services_update_title
  • common_google_play_services_updating_text
  • common_google_play_services_wear_update_text

您可以在 strings.xml 文件中覆盖它们。