//Get whether the Huawei Health app authorise access to HealthKit.
        //After calling this function, return true means authorised, false means not authorised.

        public async void GetAuthorization()
        {
            // get whether the Huawei Health app authorise access to HealthKit.
            // After calling this function, return true means authorised, false means not authorised.
            var GetAuthTask = MySettingController.GetHealthAppAuthorizationAsync();

            try
            {
                await GetAuthTask;

                if (GetAuthTask.IsCompleted)
                {
                    if (GetAuthTask.Exception == null)
                    {
                        Logger("getHealthAppAuthorisation is completed ");
                    }
                    else
                    {
                        PrintFailureMessage(GetAuthTask.Exception, "getHealthAppAuthorisation");
                    }
                }
            }
            catch (System.Exception ex)
            {
                PrintFailureMessage(ex, "getHealthAppAuthorisation");
            }
        }
Exemplo n.º 2
0
        //Check HUAWEI Health authorization status.
        //if not, start HUAWEI Health authorization Activity for user authorization.
        public async void CheckOrAuthorizeHealth()
        {
            Log.Debug(TAG, "Begint to checkOrAuthorizeHealth");
            // 1. Build a PopupWindow as progress dialog for time-consuming operation
            popupWindow = InitPopupWindow();

            // 2. Calling SettingController to query HUAWEI Health authorization status.
            // This method is asynchronous Task.
            var AuthTask = MySettingController.GetHealthAppAuthorizationAsync();

            try
            {
                await AuthTask;

                if (AuthTask.IsCompleted)
                {
                    if (AuthTask.Exception == null)
                    {
                        popupWindow.Dismiss();

                        Log.Info(TAG, "CheckOrAuthorizeHealth get result success");

                        // If HUAWEI Health is authorized, build success View.
                        if (AuthTask.Result)
                        {
                            BuildSuccessView();
                        }
                        else
                        {
                            // If not, start HUAWEI Health authorization Activity by schema with User-defined requestCode.
                            Android.Net.Uri healthKitSchemaUri = Android.Net.Uri.Parse(Constants.HEALTH_APP_SETTING_DATA_SHARE_HEALTHKIT_ACTIVITY_SCHEME);
                            Intent          intent             = new Intent(Intent.ActionView, healthKitSchemaUri);
                            // Before start, Determine whether the HUAWEI health authorization Activity can be opened.
                            if (intent.ResolveActivity(PackageManager) != null)
                            {
                                StartActivityForResult(intent, Constants.RequestHealthAuth);
                            }
                            else
                            {
                                BuildFailView(Constants.AppHealthNotInstalled);
                            }
                        }
                    }
                    else
                    {
                        popupWindow.Dismiss();

                        // The method has encountered an exception. Show exception tips in the View.
                        Log.Info(TAG, "CheckOrAuthorizeHealth has exception");
                        BuildFailView(AuthTask.Exception.Message);
                    }
                }
            }
            catch (System.Exception ex)
            {
                popupWindow.Dismiss();

                Log.Info(TAG, "CheckOrAuthorizeHealth has exception");
                BuildFailView(ex.Message);
            }
        }