应用调用此接口可以查询当前设备的运行温度,温度数据包含“高”、“中”、“低”3档。
为了使游戏应用与手机系统深度配合,游戏可以根据设备的温度主动适配系统状态,从而保障用户的流程体验,应用可以在切换到需要消耗较高资源的游戏场景时,提前调用此接口查询设备的温度。接口调用的时机请开发者自行根据游戏的场景自行设计。
● 本接口只支持特定的华为手机(目前支持EMUI8.2及以上版本),并且HMS Core(APK)的版本需要大于2.6.1。
- HMSAgent.Game.getTemperature(GetTemperatureHandler handler);
| 参数名称 | 必选(M)/可选(O) | 类型 | 参数说明 |
handler | M | 接口调用后的回调结果,查询结束后通过此回调实例回调给应用。回调结果中包含的具体参数参见结果回调。 |
- public interface GetTemperatureHandler {
- /**
- * 结果回调
- * @param retCode 返回码
- * @param result 查询结果信息
- */
- void onResult(int retCode, TemperatureResult result);
- }
| 参数名称 | 必选(M)/可选(O) | 类型 | 参数说明 |
retCode | M | int | 回调结果中查询结果的返回码,应用可以根据返回码判断查询是否成功或失败后如何处理,具体返回码处理参见返回码。 |
result | M | 包含温度信息查询结果的对象信息。 获取result对象中参数值时,请使用“对象名称.get参数名称()”的方式,参数名首字母大写。例如,“result.getTemperature()”为获取到的温度数据。 |
| 参数名称 | 必选(M)/可选(O) | 类型 | 参数说明 |
temperature | M | string | 当前设备的温度数据,值为JSON字符串,格式为“{"temperature":value}”,value取值范围如下: ● 1:低 ● 2:中 ● 3:高 应用可以使用JSONObject对象从JSON字符串中解析温度数据。 当温度值为“3”时,应用需要根据实际情况降低游戏性能的参数,例如画质、特效等,以保障游戏的流畅度。 |
| 类型 | 返回码常量 | 返回码值 | 返回码说明 | 处理建议 |
通用返回码 | 参见通用返回码。 | |||
业务返回码 | GameStatusCodes.GAME_STATE_SUCCESS | 0 | 查询成功。 | |
GameStatusCodes.GAME_STATE_NOT_REGISTER | 7015 | 未注册硬件能力。 | 请确保调用registerHardwareCapability接口,并且该接口已经注册成功。 | |
- private void getTemperature() {
- if(!isRegister) {
- return;
- }
- showLog("getTemperature: begin");
- HMSAgent.Game.getTemperature(new GetTemperatureHandler() {
- @Override
- public void onResult(int retCode, TemperatureResult result) {
- showLog("getTemperature: onResult=" + retCode);
- if(retCode == GameStatusCodes.GAME_STATE_SUCCESS && result != null) {
- showLog("getTemperature: onResult=" + result.getTemperature());
- String temperature = result.getTemperature();
- if(!TextUtils.isEmpty(temperature)) {
- try {
- JSONObject object = new JSONObject(temperature);
- int temperatureInfo = (int) object.getInt("temperature");
- if(temperatureInfo == 1) {
- //温度低
- } else if(temperatureInfo == 2) {
- //温度中
- } else if(temperatureInfo == 3) {
- //温度高
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- }
- });
- }