兒童手表定位,活動(dòng)范圍監(jiān)控—修改版
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
前言
為什么還會(huì)有修改版的兒童手表定位,活動(dòng)范圍監(jiān)控呢?因?yàn)樯弦粋€(gè)項(xiàng)目雖然已經(jīng)實(shí)現(xiàn)了,但是不實(shí)用。原因就是如果不是特地去查詢?cè)摰胤降慕?jīng)緯度,沒(méi)有誰(shuí)會(huì)準(zhǔn)確知道這個(gè)地方的經(jīng)緯度,那么就不能設(shè)定活動(dòng)范圍了。再者,上一個(gè)項(xiàng)目的活動(dòng)范圍設(shè)置只能是矩形范圍,但是只有極少數(shù)的地方是規(guī)則的矩形范圍,所以并不實(shí)用。所以針對(duì)此原因,并且對(duì)場(chǎng)景進(jìn)行細(xì)分,有了如下兩個(gè)修改版:
場(chǎng)景一:小孩在小區(qū)玩,家長(zhǎng)設(shè)定活動(dòng)范圍即為該小區(qū),孩子離開(kāi)小區(qū)時(shí)提醒家長(zhǎng)。
場(chǎng)景二:孩子和家長(zhǎng)一起外出,家長(zhǎng)設(shè)定孩子離自己的距離,超過(guò)該設(shè)定距離時(shí)提醒家長(zhǎng)。
概述
場(chǎng)景一效果圖如下:
場(chǎng)景二效果圖如下:
正文
一、實(shí)現(xiàn)場(chǎng)景一
1. 復(fù)制上一個(gè)項(xiàng)目代碼
創(chuàng)建一個(gè)名為ChildrenLocation2的Empty Java Phone應(yīng)用。

按兒童手表定位,活動(dòng)范圍監(jiān)控完善ChildrenLocation2。
2. 修改家長(zhǎng)端設(shè)備界面布局
在ability_phone.xml中編寫(xiě)以下代碼。
刪除三個(gè)文本輸入框,并修改標(biāo)識(shí)符id。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="top|center"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:Longitude"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:Latitude"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:CountryName"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:PlaceName"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"
- ohos:multiple_lines="true"/>
- <TextField
- ohos:id="$+id:tf_Location"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:hint="請(qǐng)輸入活動(dòng)位置......"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#BEBEBE"
- ohos:multiple_lines="true"/>
- <Text
- ohos:id="$+id:IDIDID"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"
- ohos:multiple_lines="true"/>
- </DirectionalLayout>
3. 重寫(xiě)getRange()函數(shù)。
在MainAbilitySlice.java中編寫(xiě)以下代碼。
通過(guò)getText()方法獲取設(shè)置的活動(dòng)范圍,通過(guò)contains()方法判斷PlaceName與設(shè)置的活動(dòng)范圍的關(guān)系,如果PlaceName不在設(shè)置的活動(dòng)范圍內(nèi)則通過(guò)startAbility方法播放音頻,否則通過(guò)stopAbility方法停止播放音頻。
- private void getRange(){
- TextField tf_Location = (TextField) findComponentById(ResourceTable.Id_tf_Location);
- Text text = (Text) findComponentById(ResourceTable.Id_IDIDID);
- String Loc = tf_Location.getText();
- Intent serviceintent = new Intent();
- Operation serviceOperation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName(getBundleName())
- .withAbilityName(ServiceAbility.class.getName())
- .build();
- serviceintent.setOperation(serviceOperation);
- if(!PlaceName.equals("null")){
- if(Loc.equals("")){
- text.setText("沒(méi)有設(shè)定范圍");
- stopAbility(serviceintent);
- }else {
- if(PlaceName.contains(Loc)){
- text.setText("孩子沒(méi)有超出設(shè)定范圍");
- stopAbility(serviceintent);
- }else {
- text.setText("孩子已超出設(shè)定范圍");
- startAbility(serviceintent);
- }
- }
- }else {
- text.setText("無(wú)法獲取孩子的位置");
- stopAbility(serviceintent);
- }
- }
二、實(shí)現(xiàn)場(chǎng)景二
1. 復(fù)制上一個(gè)項(xiàng)目代碼
創(chuàng)建一個(gè)名為ChildrenLocation3的Empty Java Phone應(yīng)用。

按兒童手表定位,活動(dòng)范圍監(jiān)控完善ChildrenLocation2。
2. 修改家長(zhǎng)端設(shè)備界面布局
在ability_phone.xml中編寫(xiě)以下代碼。
刪除三個(gè)文本輸入框,并修改標(biāo)識(shí)符id。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="top|center"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:Longitude"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:Latitude"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:CountryName"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"/>
- <Text
- ohos:id="$+id:PlaceName"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"
- ohos:multiple_lines="true"/>
- <TextField
- ohos:id="$+id:tf_Distance"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:hint="請(qǐng)輸入距離......"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#BEBEBE"/>
- <Text
- ohos:id="$+id:IDIDID"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="---"
- ohos:text_size="28fp"
- ohos:text_color="#000000"
- ohos:text_alignment="left|vertical_center"
- ohos:background_element="#78C6C5"
- ohos:multiple_lines="true"/>
- </DirectionalLayout>
3. 重寫(xiě)getRange()函數(shù)。
在MainAbilitySlice.java中編寫(xiě)以下代碼。
添加兩個(gè)變量Longitude_phone和Latitude_phone,并初始化為“null”,用于記錄家長(zhǎng)端設(shè)備的經(jīng)緯度信息。通過(guò)location.getLongitude()方法獲取經(jīng)度信息,通過(guò)location.getLatitude()方法獲取緯度信息。
通過(guò)getText()方法獲取設(shè)置的活動(dòng)距離,通過(guò)兩個(gè)位置的經(jīng)緯度計(jì)算其距離,并與設(shè)置的活動(dòng)距離進(jìn)行比較大小。根據(jù)大小關(guān)系通過(guò)startAbility方法播放音頻,通過(guò)stopAbility方法停止播放音頻。
- private static String Longitude_phone = "null";
- private static String Latitude_phone = "null";
- private void getLocation(){
- writeData("Longitude", Longitude);
- writeData("Latitude", Latitude);
- writeData("PlaceName", PlaceName);
- writeData("CountryName", CountryName);
- timer_phone = new Timer();
- timer_phone.schedule(new TimerTask() {
- @Override
- public void run() {
- getUITaskDispatcher().asyncDispatch(new Runnable() {
- @Override
- public void run() {
- getRange();
- getSingleLocation();
- }
- });
- }
- },0,5000);
- }
- private void getRange(){
- TextField tf_Distance = (TextField) findComponentById(ResourceTable.Id_tf_Distance);
- Text text = (Text) findComponentById(ResourceTable.Id_IDIDID);
- double Dis;
- if(location == null){
- Longitude_phone = "null";
- Latitude_phone = "null";
- return;
- }
- Longitude_phone = Double.toString(location.getLongitude());
- Latitude_phone = Double.toString(location.getLatitude());
- if(tf_Distance.getText().equals("")){
- Dis = -1;
- }else {
- Dis = Double.parseDouble(tf_Distance.getText());
- }
- Intent serviceintent = new Intent();
- Operation serviceOperation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName(getBundleName())
- .withAbilityName(ServiceAbility.class.getName())
- .build();
- serviceintent.setOperation(serviceOperation);
- if(!(Longitude.equals("null") && Latitude.equals("null") && Longitude_phone.equals("null") && Latitude_phone.equals("null"))){
- double Lon = getRadian(Double.parseDouble(Longitude));
- double Lat = getRadian(Double.parseDouble(Latitude));
- double Lon_phone = getRadian(Double.parseDouble(Longitude_phone));
- double Lat_phone = getRadian(Double.parseDouble(Latitude_phone));
- double distance = 2 * 6371.393 * 1000 * Math.asin(Math.sqrt(Math.sin((Lat_phone - Lat) / 2) * Math.sin((Lat_phone - Lat) / 2)) + Math.cos(Lat) * Math.cos(Lat_phone) * Math.sqrt(Math.sin((Lon_phone - Lon) / 2) * Math.sin((Lon_phone - Lon) / 2)));
- distance = Double.parseDouble(Double.toString(distance).substring(0,7));
- if(Dis != -1){
- if(distance <= Dis){
- text.setText("你距離你的孩子" + distance + "米,沒(méi)有超出距離");
- stopAbility(serviceintent);
- }else {
- text.setText("你距離你的孩子" + distance + "米,已經(jīng)超出距離");
- startAbility(serviceintent);
- }
- }else {
- text.setText("你距離你的孩子" + distance + "米,你沒(méi)有設(shè)定距離");
- stopAbility(serviceintent);
- }
- }else {
- text.setText("沒(méi)有獲取位置");
- stopAbility(serviceintent);
- }
- }
- private double getRadian(double degree){//轉(zhuǎn)化為弧度制
- return degree / 180 * Math.PI;
- }
文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)