From 38009a5929e3e05dcae1c32cfe6cab5acf92c156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Wed, 1 Nov 2023 10:30:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/build.gradle | 4 + .../bjca/trust/hm/demo/common/GsonImplHelp.java | 54 ++++++++++ .../cn/org/bjca/trust/hm/demo/common/Json.java | 29 ++++++ .../trust/hm/demo/slice/LocationAbilitySlice.java | 112 ++++++++++++++++++++- entry/src/main/js/default/pages/more/more.css | 10 +- entry/src/main/js/default/pages/more/more.hml | 4 +- .../main/resources/base/layout/ability_main.xml | 60 +++++++++-- entry/src/main/resources/zh_CN/element/string.json | 2 +- 8 files changed, 257 insertions(+), 18 deletions(-) create mode 100644 entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java create mode 100644 entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java diff --git a/entry/build.gradle b/entry/build.gradle index c002e92..33f51f0 100644 --- a/entry/build.gradle +++ b/entry/build.gradle @@ -27,6 +27,10 @@ dependencies { implementation 'com.huawei.hms:location-harmony:6.7.0.300' // agconnect依赖组件 implementation 'com.huawei.agconnect:agconnect-core-harmony:1.1.0.300' + + implementation 'com.google.code.gson:gson:2.8.5' + implementation 'com.orhanobut:logger:2.2.0' + } decc { supportType = ['html', 'xml'] diff --git a/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java b/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java new file mode 100644 index 0000000..083d60f --- /dev/null +++ b/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java @@ -0,0 +1,54 @@ +package cn.org.bjca.trust.hm.demo.common; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by xuqm on 2016/6/3. + */ +public class GsonImplHelp extends Json { + private Gson gson = new Gson(); + + @Override + public String toJson(Object src) { + return gson.toJson(src); + + } + + @Override + public T toObject(String json, Class claxx) { + return gson.fromJson(json, claxx); + + } + + @Override + public T toObject(byte[] bytes, Class claxx) { + return gson.fromJson(new String(bytes), claxx); + + } + + public List toList(String json, Class clazz) { + JsonArray jsonArray = new JsonParser().parse(json).getAsJsonArray(); + + List list = new ArrayList<>(); + for (JsonElement jsonElement : jsonArray) { + list.add(gson.fromJson(jsonElement, clazz)); //cls + } + + return list; + + + } + + public static List stringToArray(String s, Class cls) { + T[] array = new Gson().fromJson(s, cls); + return Arrays.asList(array); + } + +} diff --git a/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java b/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java new file mode 100644 index 0000000..7875b27 --- /dev/null +++ b/entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java @@ -0,0 +1,29 @@ +package cn.org.bjca.trust.hm.demo.common; + +import java.util.List; + +/** + * Created by xuqm on 2016/6/3. + */ +public abstract class Json { + private static Json json; + + Json() { + } + + public static Json get() { + if (json == null) { + json = new GsonImplHelp(); + } + return json; + } + + public abstract String toJson(Object src); + + public abstract T toObject(String json, Class claxx); + + public abstract T toObject(byte[] bytes, Class claxx); + + public abstract List toList(String json, Class claxx); + +} diff --git a/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java index 3f35288..3cdc441 100644 --- a/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java +++ b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java @@ -1,7 +1,10 @@ package cn.org.bjca.trust.hm.demo.slice; import cn.org.bjca.trust.hm.demo.ResourceTable; +import cn.org.bjca.trust.hm.demo.common.GsonImplHelp; +import com.huawei.hms.location.harmony.*; import ohos.aafwk.content.Intent; +import ohos.agp.components.Switch; import ohos.agp.components.Text; import ohos.hiviewdfx.HiLog; @@ -9,6 +12,9 @@ public class LocationAbilitySlice extends BaseAbilitySlice { private static final String TAG = "LocationAbilitySlice"; private Text showLocation; + private SettingsProviderClient settingsProviderClient; + private LocationRequest locationRequest; + private int number = 0; @Override public void onStart(Intent intent) { @@ -17,25 +23,125 @@ public class LocationAbilitySlice extends BaseAbilitySlice { checkSelfPermission(); showLocation = (Text) findComponentById(ResourceTable.Id_text_helloworld); showLocation.setClickedListener(component -> { + }); + settingsProviderClient = new SettingsProviderClient(this); + + findComponentById(ResourceTable.Id_get_settings).setClickedListener(v -> { + checkLocationSettings(); + }); + initRequest(); + ((Switch) findComponentById(ResourceTable.Id_request)).setCheckedStateChangedListener((absButton, b) -> { + if (b) { + number = 1; + fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback) + .addOnSuccessListener(v -> { + showLocation.setText("持续获取位置信息,开启完成"); + }) + .addOnFailureListener(e -> { + showLocation.setText("持续获取位置信息,开启失败:" + GsonImplHelp.get().toJson(e)); + }); + } else { + // 注意:停止位置更新时,mLocationCallback与requestLocationUpdates()中的LocationCallback参数为同一对象。 + fusedLocationClient.removeLocationUpdates(locationCallback) + .addOnSuccessListener(v -> { + showLocation.setText("持续获取位置信息,关闭完成"); + }) + .addOnFailureListener(e -> { + showLocation.setText("持续获取位置信息,关闭失败:" + GsonImplHelp.get().toJson(e)); + }); + } + }); + findComponentById(ResourceTable.Id_get_last).setClickedListener(v -> { + getLastLocation(); }); + findComponentById(ResourceTable.Id_get_last_address).setClickedListener(v -> { + fusedLocationClient.getLastLocationWithAddress(buildLocationRequest()) + .addOnSuccessListener(location -> { + if (null == location) { + return; + } + showLocation.setText("获取最后一次位置信息完成:" + GsonImplHelp.get().toJson(location)); + }) + .addOnFailureListener(e -> { + showLocation.setText("获取最后一次位置信息失败:" + GsonImplHelp.get().toJson(e)); + }); + + }); + } + + private void initRequest() { + locationRequest = new LocationRequest(); +// 设置位置更新的间隔(毫秒:单位) + locationRequest.setInterval(2000); +// 设置权重 + locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); +// (可选)设置是否需要返回地址信息 + locationRequest.setNeedAddress(true); +// (可选)设置返回地址信息的语言 + locationRequest.setLanguage("zh"); + } + + private LocationCallback locationCallback = new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + if (locationResult != null) { + showLocation.setText("持续获取位置信息,第" + number + "次:" + GsonImplHelp.get().toJson(locationResult)); + number += 1; + } + } + + @Override + public void onLocationAvailability(LocationAvailability locationAvailability) { + super.onLocationAvailability(locationAvailability); + if (locationAvailability != null) { + // 处理位置状态 + } + } + }; + + private void checkLocationSettings() { + LocationRequest locationRequest = new LocationRequest(); + locationRequest.setPriority(100); + LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); + LocationSettingsRequest request = + builder.addLocationRequest(locationRequest).setAlwaysShow(false).setNeedBle(false).build(); + settingsProviderClient.checkLocationSettings(request) + .addOnSuccessListener(response -> { + // 设置成功定位条件 + showLocation.setText("成功:" + GsonImplHelp.get().toJson(response)); + }) + .addOnFailureListener(exp -> { + // 设置不满足定位条件 + showLocation.setText("失败:" + GsonImplHelp.get().toJson(exp)); + }); } private void getLastLocation() { + showLocation.setText("logs:"); fusedLocationClient.getLastLocation().addOnSuccessListener(location -> { if (null == location) { printLog(HiLog.INFO, TAG, "[old]getLastLocation onSuccess: location is null"); return; } - String result = "[old]getLastLocation onSuccess location[Longitude,Latitude]:" + location.getLongitude() + "," + location.getLatitude(); + String result = "获取最后有效位置成功 onSuccess location[Longitude,Latitude]:" + location.getLongitude() + "," + location.getLatitude(); printLog(HiLog.INFO, TAG, result); printScreenLog(showLocation, result); }).addOnFailureListener(e -> { - printLog(HiLog.INFO, TAG, "getLastLocation onFailure:" + e.getMessage()); - printScreenLog(showLocation, "getLastLocation onFailure:" + e.getMessage()); + printLog(HiLog.INFO, TAG, "获取最后有效位置失败:" + e.getMessage()); + printScreenLog(showLocation, "获取最后有效位置失败:" + e.getMessage()); }); } + private LocationRequest buildLocationRequest() { + LocationRequest locationRequest = new LocationRequest(); + locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + locationRequest.setInterval(5000); + locationRequest.setNeedAddress(true); + locationRequest.setLanguage("zh"); + return locationRequest; + } + @Override public void onActive() { super.onActive(); diff --git a/entry/src/main/js/default/pages/more/more.css b/entry/src/main/js/default/pages/more/more.css index 2dda1ec..4490ad0 100644 --- a/entry/src/main/js/default/pages/more/more.css +++ b/entry/src/main/js/default/pages/more/more.css @@ -4,7 +4,7 @@ margin-top: 10px; } .listG{ - height: 400px; + height: 100%; } .listItem{ width: 100%; @@ -16,17 +16,17 @@ } .group{ width: 96%; - height: 60px; + height: 90px; padding-left: 3%; margin-left: 6%; border-bottom: 1px solid #DEDEDE; - font-size: 20px; + font-size: 30px; font-weight:500; } .groupValue{ - font-size: 16px; + font-size: 24px; width: 95%; - height: 60px; + height: 90px; margin-left: 15%; border-bottom: 1px solid #DEDEDE; } \ No newline at end of file diff --git a/entry/src/main/js/default/pages/more/more.hml b/entry/src/main/js/default/pages/more/more.hml index 7ed5352..52558bf 100644 --- a/entry/src/main/js/default/pages/more/more.hml +++ b/entry/src/main/js/default/pages/more/more.hml @@ -6,13 +6,13 @@ 地图 - 广告位招租 + 支付相关 文本内容 文本内容 文本内容 - 分组列表 + 扫码相关 文本内容 文本内容 文本内容 diff --git a/entry/src/main/resources/base/layout/ability_main.xml b/entry/src/main/resources/base/layout/ability_main.xml index 05bd6c0..f0a1e0d 100644 --- a/entry/src/main/resources/base/layout/ability_main.xml +++ b/entry/src/main/resources/base/layout/ability_main.xml @@ -3,17 +3,63 @@ xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" - ohos:alignment="center" ohos:orientation="vertical"> + +