【问题标题】:Is it possible to detect if Google Fit app is installed and used by user?是否可以检测用户是否安装并使用了 Google Fit 应用程序?
【发布时间】:2015-12-15 14:52:01
【问题描述】:

我想在我的应用中添加计步器。 到目前为止,我正在使用Google fit 来测量用户步数。

我面临的问题是并非所有设备都安装了Google Fit 应用程序,即使没有 - 并非所有用户都注册到应用程序服务(作为客户端)。

所以想要的流程是:

  1. 检测是否安装了Google fit 应用。
  2. 如果已安装,请检查用户是否已注册到Google fit 应用。
  3. 如果已安装并注册到Google fit 应用程序,请检查用户帐户类型支持是否使用健身服务。 (记录步数等数据)
  4. 如果以上都ok,检查用户是否确认了`Google fit弹窗。

基本上我想检查用户是否使用 fit 应用程序(使用上述所有条件),如果一个失败,那么它将使用设备上的 StepCount 传感器(如果存在),如果传感器不存在,它将使用另一个传感器实现这一目标。

这是我用来连接 Google Fit API 的代码:

private void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(getContext())
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {

                        @Override
                        public void onConnected(Bundle bundle) {
                            Log.i(TAG, "Connected!!!");
                            // Now you can make calls to the Fitness APIs.
                            // Put application specific code here.


                            new getFitnessTask().execute();
                        }


                        @Override
                        public void onConnectionSuspended(int i) {
                            // If your connection to the sensor gets lost at some point,
                            // you'll be able to determine the reason and react to it here.
                            if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                            } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                Log.i(TAG, "Connection lost.  Reason: Service Disconnected");
                            }
                        }
                    }
            )
            .addOnConnectionFailedListener(
                    new GoogleApiClient.OnConnectionFailedListener() {
                        // Called whenever the API client fails to connect.
                        @Override
                        public void onConnectionFailed(ConnectionResult result) {
                            Log.i(TAG, "Connection failed. Cause: " + result.toString());
                            if (!result.hasResolution()) {
                                // Show the localized error dialog
                                if (getActivity() != null) {

                                    GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
                                            getActivity(), 0).show();
                                }
                                return;
                            }
                            // The failure has a resolution. Resolve it.
                            // Called typically when the app is not yet authorized, and an
                            // authorization dialog is displayed to the user.
                            if (!authInProgress) {

                                try {
                                    Log.i(TAG, "Attempting to resolve failed connection; activity; "+  getActivity());
                                    if (getActivity() != null) {

                                        authInProgress = true;
                                        result.startResolutionForResult(getActivity(),
                                                REQUEST_OAUTH);
                                    }
                                } catch (IntentSender.SendIntentException e) {
                                    Log.e(TAG,
                                            "Exception while starting resolution activity", e);
                                }
                            }
                        }
                    }
            )
            .build();
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    Log.e(TAG, "onActivityResult requestCode=" + requestCode + ", resultCode=" + resultCode);
    if (requestCode == REQUEST_OAUTH) {
        authInProgress = false;
        if (resultCode == Activity.RESULT_OK) {

            Log.e(TAG, "RESULT_OK");
            // Make sure the app is not already connected or attempting to connect
            if (!mClient.isConnecting() && !mClient.isConnected()) {
                mClient.connect();
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.e(TAG, "RESULT_CANCELED");
        }
    }
}


private class getFitnessTask extends AsyncTask<Void, Void, Integer> {
    ...
}
}

需要你们的帮助,

谢谢。

【问题讨论】:

  • 我也遇到了同样的问题,有什么解决办法吗?
  • 也在寻找同样问题的解决方案。如果您已经得到答案,请发布答案。
  • this question密切相关

标签: android google-fit


【解决方案1】:

您可以在PackageManager 上致电getPackageInfo()。如果它抛出PackageManager.NameNotFoundException,则表示该应用程序未安装。如果没有抛出异常,则安装了 google fit

private static final String PACKAGE_NAME = "com.google.android.apps.fitness";

@CheckResult public boolean fitInstalled() {
    try {
        context.getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

【讨论】:

    【解决方案2】:

    这里是 kotlin 版本

    fun checkIfFitInstalled(): Boolean {
        try {
            applicationContext.packageManager.getPackageInfo("com.google.android.apps.fitness", PackageManager.GET_ACTIVITIES)
            return true
        } catch (e: Exception) {
            return false
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多