Procházet zdrojové kódy

定位完成

master
徐勤民 před 10 měsíci
rodič
revize
38009a5929
8 změnil soubory, kde provedl 257 přidání a 18 odebrání
  1. +4
    -0
      entry/build.gradle
  2. +54
    -0
      entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java
  3. +29
    -0
      entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java
  4. +109
    -3
      entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java
  5. +5
    -5
      entry/src/main/js/default/pages/more/more.css
  6. +2
    -2
      entry/src/main/js/default/pages/more/more.hml
  7. +53
    -7
      entry/src/main/resources/base/layout/ability_main.xml
  8. +1
    -1
      entry/src/main/resources/zh_CN/element/string.json

+ 4
- 0
entry/build.gradle Zobrazit soubor

@@ -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']


+ 54
- 0
entry/src/main/java/cn/org/bjca/trust/hm/demo/common/GsonImplHelp.java Zobrazit soubor

@@ -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> T toObject(String json, Class<T> claxx) {
return gson.fromJson(json, claxx);

}

@Override
public <T> T toObject(byte[] bytes, Class<T> claxx) {
return gson.fromJson(new String(bytes), claxx);

}

public <T> List<T> toList(String json, Class<T> clazz) {
JsonArray jsonArray = new JsonParser().parse(json).getAsJsonArray();

List<T> list = new ArrayList<>();
for (JsonElement jsonElement : jsonArray) {
list.add(gson.fromJson(jsonElement, clazz)); //cls
}

return list;


}

public static <T> List<T> stringToArray(String s, Class<T[]> cls) {
T[] array = new Gson().fromJson(s, cls);
return Arrays.asList(array);
}

}

+ 29
- 0
entry/src/main/java/cn/org/bjca/trust/hm/demo/common/Json.java Zobrazit soubor

@@ -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> T toObject(String json, Class<T> claxx);

public abstract <T> T toObject(byte[] bytes, Class<T> claxx);

public abstract <T> List<T> toList(String json, Class<T> claxx);

}

+ 109
- 3
entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java Zobrazit soubor

@@ -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();


+ 5
- 5
entry/src/main/js/default/pages/more/more.css Zobrazit soubor

@@ -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;
}

+ 2
- 2
entry/src/main/js/default/pages/more/more.hml Zobrazit soubor

@@ -6,13 +6,13 @@
<list-item><text class="groupValue">地图</text></list-item>
</list-item-group>
<list-item-group>
<list-item><text class="group">广告位招租</text></list-item>
<list-item><text class="group">支付相关</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>
</list-item-group>
<list-item-group>
<list-item><text class="group">分组列表</text></list-item>
<list-item><text class="group">扫码相关</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>
<list-item><text class="groupValue">文本内容</text></list-item>


+ 53
- 7
entry/src/main/resources/base/layout/ability_main.xml Zobrazit soubor

@@ -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">


<Button
ohos:id="$+id:get_settings"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="检查设置"
ohos:text_size="20vp"
ohos:top_margin="10vp"/>

<DirectionalLayout
ohos:layout_alignment="center"
ohos:height="match_content"
ohos:width="match_content"
ohos:orientation="horizontal"
ohos:start_margin="10vp">

<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="持续获取位置信息"
ohos:text_size="20vp"/>

<Switch
ohos:id="$+id:request"
ohos:height="20vp"
ohos:width="50vp"
ohos:start_margin="10vp"
ohos:text_color="#ffffff"
ohos:text_state_off="关闭"
ohos:text_state_on="开启"/>
</DirectionalLayout>

<Button
ohos:id="$+id:get_last"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="获取最后的已知位置"
ohos:text_size="20vp"
ohos:top_margin="10vp"/>

<Button
ohos:id="$+id:get_last_address"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="获取最后详细信息的已知位置"
ohos:text_size="20vp"
ohos:top_margin="10vp"/>

<Text
ohos:id="$+id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:locationability_HelloWorld"
ohos:text_size="40vp"
ohos:width="match_parent"
ohos:multiple_lines="true"
ohos:top_margin="20vp"
ohos:text="log:"
ohos:text_size="15vp"
/>

</DirectionalLayout>

+ 1
- 1
entry/src/main/resources/zh_CN/element/string.json Zobrazit soubor

@@ -22,7 +22,7 @@
},
{
"name": "entry_LocationAbility",
"value": "entry_LocationAbility"
"value": "华为定位相关"
}
]
}

Načítá se…
Zrušit
Uložit