目录

SettingController

Class Info

public class SettingController

Provides the setting-related functions.

Public Method Summary

Qualifier and Type

Method Name and Description

Task<DataType>

addDataType(DataTypeAddOptions options)

Creates and adds a customized data type.

Task<Void>

disableHiHealth()

Disables Health Service Kit.

Task<Boolean>

getHealthAppAuthorization()

Checks whether linking of Huawei Health to Health Service Kit is enabled in the privacy management of Huawei Health.

HealthKitAuthResult

parseHealthKitAuthResultFromIntent(Intent intent)

Parses the authorization result from the intent returned in the sign-in authorization process.

Task<DataType>

readDataType(String dataTypeName)

Reads the corresponding customized data type based on the data type name.

Intent

requestAuthorizationIntent(String[] scopes, boolean enableHealthAuth)

Obtains the intent of integrating the HUAWEI ID sign-in authorization process and Huawei Health authorization process, and uses startActivityForResult to initiate the integrated authorization process.

Public Methods

[h2]addDataType(DataTypeAddOptions options)

Method

public Task<DataType> addDataType(DataTypeAddOptions options)

Creates and adds a customized data type. The name of the created data type must be prefixed with the package name of the app. Otherwise, the creation fails.

Parameters

Name

Description

options

Request options for creating the data type.

Returns

Type

Description

Task<DataType>

Data type that is successfully created and added.

The sample code for creating and adding a customized data type is as follows:
Java
public void addNewDataType(View view) {
    // The new name must start with the package name and end with a customized name.
    String dataTypeName = "com.myproject.demo";

    // Create the field values to be added.
    Field selectedField = Field.newDoubleField("myField");
    // Create DataTypeAddOptions.
    DataTypeAddOptions dataTypeAddOptions =
        new DataTypeAddOptions.Builder().setName(dataTypeName).addField(selectedField).build();

    // Add a new data type.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this)
        .addDataType(dataTypeAddOptions)
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.i("TAG", "Add dataType failed");
            }
        })
        .addOnCompleteListener(new OnCompleteListener<DataType>() {
            @Override
            public void onComplete(Task<DataType> task) {
                String res = task.isSuccessful() ? "success" : "failed";
                Log.i("TAG", "Add dataType " + res);
                if (task.getException() != null) {
                    Log.i("TAG", "GetException is " + task.getException().toString());
                }
            }
        });
}
Kotlin
fun addNewDataType(view: View) {
    // The new name must start with the package name and end with a customized name.
    val dataTypeName = "com.myproject.demo"
    // Create the field values to be added.
    val selectedField = Field.newDoubleField("myField")
    // Create DataTypeAddOptions.
    
    val dataTypeAddOptions =
        DataTypeAddOptions.Builder().setName(dataTypeName).addField(selectedField).build()

    // Create a domain to be added and add a new data type.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this).addDataType(dataTypeAddOptions)
        .addOnFailureListener { e -> printFailureMessage(e, "addNewDataType") }
        .addOnCompleteListener { task ->
            val res = if (task.isSuccessful) "success" else "failed"
            Log.i("TAG","add dataType of $selectedFieldStr is $res")
            if (task.exception != null) {
                Log.i("TAG","getException is " + task.exception.toString())
            }
        }
    }
}

[h2]disableHiHealth()

Method

public Task<Void> disableHiHealth()

Disables the Health Service Kit function, cancels the user authorization to Health Service Kit (which takes effect 24 hours later), and deletes all data records.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

The sample code for disabling the Health Service Kit function is as follows:
Java
public void disableHiHealth(View view) {
    // Cancel all records.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this)
        .disableHiHealth()
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.i("TAG", "Disable AutoRecorder failed");
            }
        })
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                String res = task.isSuccessful() ? "success" : "failed";
                Log.i("TAG", "Disable AutoRecorder is " + res);
            }
        });
}
Kotlin
fun disableHiHealth(view: View) {
    // Cancel all records.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this).disableHiHealth()
        .addOnFailureListener { e -> printFailureMessage(e, "readTodaySummationFromDevice") }
        .addOnCompleteListener { task ->
            val res = if (task.isSuccessful) "success" else "failed"
            Log.i("TAG","disableHiHealth is $res")
        }
    }
}

[h2]readDataType(String dataTypeName)

Method

public Task<DataType> readDataType(String dataTypeName)

Reads the data type based on the data type name. This method is used to read the customized data types of the app.

Parameters

Name

Description

dataTypeName

Data type name.

Returns

Type

Description

Task<DataType>

Data type that has been read based on the data type name.

The sample code for reading the data type based on the data type name is as follows:
Java
public void readDataType(View view) {
    // Obtain the name of the data type.
    String dataTypeName = "com.myproject.demo";
    // Obtain the data type of the specified name.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this)
        .readDataType(dataTypeName)
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.i("TAG", "Read dataType failed");
            }
        })
        .addOnCompleteListener(new OnCompleteListener<DataType>() {
            @Override
            public void onComplete(Task<DataType> task) {
                Log.i("TAG", "DataType : " + task.getResult());
            }
        });
}
Kotlin
fun readDataType(view: View) {
    // Obtain the name of the data type.
    val dataTypeName = "com.myproject.demo"
    // Obtain the data type of the specified name.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this).readDataType(dataTypeName)
        .addOnFailureListener { e -> printFailureMessage(e, "readDataType") }
        .addOnCompleteListener { task -> Log.i("TAG","DataType : " + task.result) }
    }
}

[h2]getHealthAppAuthorization()

Method

public Task<Boolean> getHealthAppAuthorization()

Checks whether linking of Huawei Health to Health Service Kit is enabled in the privacy management of Huawei Health.

Returns

Type

Description

Task<Boolean>

Whether the user privacy authorization has been granted to Health Service Kit.

  • true: yes
  • false: no
The sample code for checking the user privacy authorization to Health Service Kit is as follows:
Java
public void getHealthAppAuthorization(View view) {
   // Check whether linking of Huawei Health to Health Service Kit is enabled in the privacy management of Huawei Health.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this)
        .getHealthAppAuthorization()
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.i("TAG", "getHealthAppAuthorization failed");
            }
        })
        .addOnSuccessListener(new OnSuccessListener<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                String res = result ? "success" : "failed";
                Log.i("TAG", "getHealthAppAuthorization is " + res);
            }
        });
}
Kotlin
fun getHealthAppAuthorization(view: View) {
   // Check whether linking of Huawei Health to Health Service Kit is enabled in the privacy management of Huawei Health.
    // Display the returned result on the phone screen.
    // Note that this refers to an Activity object.
    HuaweiHiHealth.getSettingController(this)
        .getHealthAppAuthorization()        
        .addOnFailureListener { e -> printFailureMessage(e, "getHealthAppAuthorization") }
        .addOnSuccessListener { result ->
            val res = if (result) "success" else "failed"
            Log.i("TAG","getHealthAppAuthorization is $res")
        }
    }
}

[h2]requestAuthorizationIntent(String[] scopes, boolean enableHealthAuth)

Method

public Intent requestAuthorizationIntent(String[] scopes, boolean enableHealthAuth)

Obtains the intent of integrating the HUAWEI ID sign-in authorization process and Huawei Health authorization process, and uses startActivityForResult to initiate the integrated authorization process.

Parameters

Name

Description

scopes

Scope array for scope application.

enableHealthAuth

Switch of Huawei Health authorization. Value true indicates that the authorization process is enabled, and value false indicates that the authorization process is disabled.

Returns

Type

Description

Intent

Intent of starting the authorization process activity, which can be initiated through startActivityForResult.

Java
// Log tag
private static final String TAG = "HealthKitAuthActivity";

// Request code for displaying the authorization screen using the startActivityForResult method. The value can be customized.
private static final int REQUEST_AUTH = 1000;

private void requestAuth() {
    // Add scopes to apply for. The following only shows an example. You need to add scopes according to your specific needs.
    String[] scopes = new String[] {
        // View and store the step count in Health Service Kit.
        Scopes.HEALTHKIT_STEP_READ, Scopes.HEALTHKIT_STEP_WRITE,
        // View and store the height and weight in Health Service Kit.
        Scopes.HEALTHKIT_HEIGHTWEIGHT_READ, Scopes.HEALTHKIT_HEIGHTWEIGHT_WRITE,
        // View and store the heart rate data in Health Service Kit.
        Scopes.HEALTHKIT_HEARTRATE_READ, Scopes.HEALTHKIT_HEARTRATE_WRITE};

    // Obtain the intent of the authorization process. Value true indicates that the authorization process of the Huawei Health app is enabled, and false indicates that the authorization process is disabled.
    // Note that this refers to an Activity object.
    Intent intent = 
HuaweiHiHealth.getSettingController(this).requestAuthorizationIntent(scopes, true);

    // Open the authorization process page. This method must be used in the activity.
    Log.i(TAG, "start authorization activity");
    startActivityForResult(intent, REQUEST_AUTH);
}
Kotlin
private fun requestAuth() {
    // Add scopes to apply for. The following only shows an example. You need to add scopes according to your specific needs.
    val scopes = arrayOf(
        // View and store the step count in Health Service Kit.
        Scopes.HEALTHKIT_STEP_READ, Scopes.HEALTHKIT_STEP_WRITE,
        // View and store the height and weight in Health Service Kit.
        Scopes.HEALTHKIT_HEIGHTWEIGHT_READ, Scopes.HEALTHKIT_HEIGHTWEIGHT_WRITE,
        // View and store the heart rate data in Health Service Kit.
        Scopes.HEALTHKIT_HEARTRATE_READ, Scopes.HEALTHKIT_HEARTRATE_WRITE
    )

    // Obtain the intent of the authorization process.
    // Note that this refers to an Activity object.
    val intent = HuaweiHiHealth.getSettingController(this).requestAuthorizationIntent(scopes, true)

    // Open the authorization process screen.
    Log.i(TAG, "start authorization activity");
    startActivityForResult(intent, REQUEST_AUTH)
}

companion object {
    // Log tag
    private val TAG = "HealthKitAuthActivity"

    // Request code for displaying the authorization screen using the startActivityForResult method. The value can be customized.
    private val REQUEST_AUTH = 1000
}

[h2]parseHealthKitAuthResultFromIntent(Intent intent)

Method

public HealthKitAuthResult parseHealthKitAuthResultFromIntent(Intent intent)

Parses the authorization result from the intent returned in the sign-in authorization process.

Parameters

Name

Description

intent

Intent returned in the sign-in authorization process.

Returns

Type

Description

HealthKitAuthResult

Authorization result parsed from the intent.

Java
// Log tag
private static final String TAG = "HealthKitAuthActivity";

// Request code for displaying the authorization screen using the startActivityForResult method. The value can be customized.
private static final int REQUEST_AUTH = 1000;

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Process only the response result of the authorization process.
    if (requestCode == REQUEST_AUTH) {
        // Obtain the authorization response result from the intent.
    // Note that this refers to an Activity object.
        HealthKitAuthResult result = HuaweiHiHealth.getSettingController(this).parseHealthKitAuthResultFromIntent(data);
        if (result == null) {
            Log.w(TAG, "authorization fail");
            return;
        }

        if (result.isSuccess()) {
            Log.i(TAG, "authorization success");
        } else {
            Log.w(TAG, "authorization fail, errorCode:" + result.getErrorCode());
        }
    }
}
Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    // Process only the response result of the authorization process.
    if (requestCode == REQUEST_AUTH) {
        // Obtain the authorization response result from the intent.
    // Note that this refers to an Activity object.
        val result = HuaweiHiHealth.getSettingController(this).parseHealthKitAuthResultFromIntent(data)
        if (result == null) {
            Log.w(TAG, "authorization fail")
            return
        }

        if (result.isSuccess) {
            Log.i(TAG, "authorization success")
        } else {
            Log.w(TAG, "authorization fail, errorCode:" + result.errorCode)
        }
    }
}

companion object {
    // Log tag
    private val TAG = "HealthKitAuthActivity"

    // Request code for displaying the authorization screen using the startActivityForResult method. The value can be customized.
    private val REQUEST_AUTH = 1000
}