【问题标题】:Android: testing logic for users who haven't got google play services, on an emulatorAndroid:在模拟器上为没有谷歌播放服务的用户测试逻辑
【发布时间】:2016-03-29 19:48:39
【问题描述】:

我正在尝试为没有在我设置的模拟器上获得谷歌播放服务的用户测试逻辑(不使用谷歌 API)。正如预期的那样,尝试使用 Google 登录会弹出“获取 google play 服务”对话框,但按下对话框中的按钮只会导致 logcat 出现错误:

SettingsRedirect:无法重定向到 Google Play 服务的应用设置

我想知道这是否仅仅是因为我使用了模拟器,或者它是否表明我的代码中存在可能影响用户的错误? (即如果有办法让这个按钮在模拟器上工作)

编辑:我检查 gps 方法的代码:

 public static boolean checkPlayServices(Activity activity, String actionWeArePerforming) {

        Timber.i("checkPlayServices: called by %s because of %s", activity.getClass().getSimpleName(), actionWeArePerforming);

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(activity);
        int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

        if (resultCode != ConnectionResult.SUCCESS) {

            Timber.d("checkPlayservices: resultcode was not success");

            if (googleApiAvailability.isUserResolvableError(resultCode)) {

                Timber.d("checkPlayServices: update available - resolving");

                // "user resolvable" means Google Play is available to download the last version of Play Services APK
                // This will open Google dialog fragment displaying the proper message depending on "resultCode"
                googleApiAvailability.showErrorDialogFragment(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST);
            } else {

                Timber.d("checkPlayServices: update not available");

                final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) activity
                        .findViewById(android.R.id.content)).getChildAt(0);

                // Should not happen. This device does not support Play Services.
                // Let's show an ultimate warning.
                Snackbar.make(viewGroup, activity.getString(R.string.cannot_download_google_play_services), Snackbar.LENGTH_LONG);

            }

            Timber.i("checkPlayServices: returning false");
            return false;
        }

        Timber.i("checkPlayServices: returning true");
        return true;
    }

【问题讨论】:

  • 你能发布一些代码吗?当模拟器中为我弹出对话框并单击它时,它不会崩溃。什么都没有发生。
  • 它对我来说也不会崩溃 - 但它不会进入任何谷歌播放服务下载页面,所以我想知道这是否是我可以用模拟器测试的逻辑范围跨度>
  • 您找到解决方案了吗?
  • @TimAutin 不幸的是,除了我在这里写的内容之外,什么都没有
  • 好的,谢谢!我想我们必须自己做......

标签: java android google-play-services emulation google-signin


【解决方案1】:

我结束了:

 private boolean checkPlayServices() {

    // Get the availability :
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int result = googleApiAvailability.isGooglePlayServicesAvailable(this);

    if(result != ConnectionResult.SUCCESS) {

        // Build a popup :
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.main_activity_google_play_services_problem_alert_title);
        builder.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                finish();
            }
        });

        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                final String appPackageName = "com.google.android.gms";
                try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); }
                catch (android.content.ActivityNotFoundException e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); }

                finish();
            }
        };

        // Set the messages according to the most common errors and show the popup :
        switch(result) {

            case ConnectionResult.SERVICE_MISSING : {

                builder.setMessage(R.string.main_activity_google_play_services_missing_alert_message);
                builder.setPositiveButton(R.string.main_activity_google_play_services_missing_alert_positive_button, listener);
                builder.show();

                return false;
            }

            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED : {

                builder.setMessage(R.string.main_activity_google_play_services_needs_update_alert_message);
                builder.setPositiveButton(R.string.main_activity_google_play_services_needs_update_alert_positive_button, listener);
                builder.show();

                return false;
            }

            default: {

                builder.setMessage(getString(R.string.main_activity_google_play_services_problem_alert_message).replaceAll("\\$1", googleApiAvailability.getErrorString(result)));
                builder.setPositiveButton(R.string.main_activity_google_play_services_problem_alert_positive_button, listener);
                builder.show();

                return false;
            }
        }
    }

    return true;
}

这样:

  • 如果可用性OK,该方法返回true
  • 如果服务未安装/需要更新/有问题,该方法会打开一个弹出窗口
  • 如果未安装 Play 商店,用户仍然可以在其浏览器中打开链接

应该没问题,我猜……可惜的是,Google 提供的开箱即用的方法只完成了一半:/

当然,您可以通过使用结果启动 Play 商店活动并再次检查 onActivityResult 来做得更好,但我一直很懒 :)

【讨论】:

    【解决方案2】:

    是模拟器的原因,看这个问题:

    https://github.com/googlesamples/google-services/issues/32

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多