成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

兒童手表定位,活動范圍監控—修改版

系統
孩子戴著手表去上補習班、在小區內玩耍等等,手表可以將實時位置發送給家長,超出設置的活動范圍內時提醒家長,實現孩子安全防患于未然。

[[420005]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

前言

為什么還會有修改版的兒童手表定位,活動范圍監控呢?因為上一個項目雖然已經實現了,但是不實用。原因就是如果不是特地去查詢該地方的經緯度,沒有誰會準確知道這個地方的經緯度,那么就不能設定活動范圍了。再者,上一個項目的活動范圍設置只能是矩形范圍,但是只有極少數的地方是規則的矩形范圍,所以并不實用。所以針對此原因,并且對場景進行細分,有了如下兩個修改版:

場景一:小孩在小區玩,家長設定活動范圍即為該小區,孩子離開小區時提醒家長。

場景二:孩子和家長一起外出,家長設定孩子離自己的距離,超過該設定距離時提醒家長。

概述

場景一效果圖如下:

場景二效果圖如下:

正文

一、實現場景一

1. 復制上一個項目代碼

創建一個名為ChildrenLocation2的Empty Java Phone應用。

兒童手表定位,活動范圍監控——修改版-鴻蒙HarmonyOS技術社區

兒童手表定位,活動范圍監控完善ChildrenLocation2。

2. 修改家長端設備界面布局

在ability_phone.xml中編寫以下代碼。

刪除三個文本輸入框,并修改標識符id。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="top|center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <Text 
  10.         ohos:id="$+id:Longitude" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_parent" 
  13.         ohos:margin="10vp" 
  14.         ohos:padding="10vp" 
  15.         ohos:text="---" 
  16.         ohos:text_size="28fp" 
  17.         ohos:text_color="#000000" 
  18.         ohos:text_alignment="left|vertical_center" 
  19.         ohos:background_element="#78C6C5"/> 
  20.  
  21.     <Text 
  22.         ohos:id="$+id:Latitude" 
  23.         ohos:height="match_content" 
  24.         ohos:width="match_parent" 
  25.         ohos:margin="10vp" 
  26.         ohos:padding="10vp" 
  27.         ohos:text="---" 
  28.         ohos:text_size="28fp" 
  29.         ohos:text_color="#000000" 
  30.         ohos:text_alignment="left|vertical_center" 
  31.         ohos:background_element="#78C6C5"/> 
  32.  
  33.     <Text 
  34.         ohos:id="$+id:CountryName" 
  35.         ohos:height="match_content" 
  36.         ohos:width="match_parent" 
  37.         ohos:margin="10vp" 
  38.         ohos:padding="10vp" 
  39.         ohos:text="---" 
  40.         ohos:text_size="28fp" 
  41.         ohos:text_color="#000000" 
  42.         ohos:text_alignment="left|vertical_center" 
  43.         ohos:background_element="#78C6C5"/> 
  44.  
  45.     <Text 
  46.         ohos:id="$+id:PlaceName" 
  47.         ohos:height="match_content" 
  48.         ohos:width="match_parent" 
  49.         ohos:margin="10vp" 
  50.         ohos:padding="10vp" 
  51.         ohos:text="---" 
  52.         ohos:text_size="28fp" 
  53.         ohos:text_color="#000000" 
  54.         ohos:text_alignment="left|vertical_center" 
  55.         ohos:background_element="#78C6C5" 
  56.         ohos:multiple_lines="true"/> 
  57.  
  58.     <TextField 
  59.         ohos:id="$+id:tf_Location" 
  60.         ohos:height="match_content" 
  61.         ohos:width="match_parent" 
  62.         ohos:margin="10vp" 
  63.         ohos:padding="10vp" 
  64.         ohos:hint="請輸入活動位置......" 
  65.         ohos:text_size="28fp" 
  66.         ohos:text_color="#000000" 
  67.         ohos:text_alignment="left|vertical_center" 
  68.         ohos:background_element="#BEBEBE" 
  69.         ohos:multiple_lines="true"/> 
  70.  
  71.     <Text 
  72.         ohos:id="$+id:IDIDID" 
  73.         ohos:height="match_content" 
  74.         ohos:width="match_parent" 
  75.         ohos:margin="10vp" 
  76.         ohos:padding="10vp" 
  77.         ohos:text="---" 
  78.         ohos:text_size="28fp" 
  79.         ohos:text_color="#000000" 
  80.         ohos:text_alignment="left|vertical_center" 
  81.         ohos:background_element="#78C6C5" 
  82.         ohos:multiple_lines="true"/> 
  83.  
  84. </DirectionalLayout> 

3. 重寫getRange()函數。

在MainAbilitySlice.java中編寫以下代碼。

通過getText()方法獲取設置的活動范圍,通過contains()方法判斷PlaceName與設置的活動范圍的關系,如果PlaceName不在設置的活動范圍內則通過startAbility方法播放音頻,否則通過stopAbility方法停止播放音頻。

  1. private void getRange(){ 
  2.       TextField tf_Location = (TextField) findComponentById(ResourceTable.Id_tf_Location); 
  3.  
  4.       Text text = (Text) findComponentById(ResourceTable.Id_IDIDID); 
  5.  
  6.       String Loc = tf_Location.getText(); 
  7.  
  8.       Intent serviceintent = new Intent(); 
  9.       Operation serviceOperation = new Intent.OperationBuilder() 
  10.               .withDeviceId(""
  11.               .withBundleName(getBundleName()) 
  12.               .withAbilityName(ServiceAbility.class.getName()) 
  13.               .build(); 
  14.       serviceintent.setOperation(serviceOperation); 
  15.  
  16.       if(!PlaceName.equals("null")){ 
  17.           if(Loc.equals("")){ 
  18.               text.setText("沒有設定范圍"); 
  19.               stopAbility(serviceintent); 
  20.           }else { 
  21.               if(PlaceName.contains(Loc)){ 
  22.                   text.setText("孩子沒有超出設定范圍"); 
  23.                   stopAbility(serviceintent); 
  24.               }else { 
  25.                   text.setText("孩子已超出設定范圍"); 
  26.                   startAbility(serviceintent); 
  27.               } 
  28.           } 
  29.       }else { 
  30.           text.setText("無法獲取孩子的位置"); 
  31.           stopAbility(serviceintent); 
  32.       } 
  33.   } 

二、實現場景二

1. 復制上一個項目代碼

創建一個名為ChildrenLocation3的Empty Java Phone應用。

兒童手表定位,活動范圍監控——修改版-鴻蒙HarmonyOS技術社區

兒童手表定位,活動范圍監控完善ChildrenLocation2。

2. 修改家長端設備界面布局

在ability_phone.xml中編寫以下代碼。

刪除三個文本輸入框,并修改標識符id。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="top|center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <Text 
  10.         ohos:id="$+id:Longitude" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_parent" 
  13.         ohos:margin="10vp" 
  14.         ohos:padding="10vp" 
  15.         ohos:text="---" 
  16.         ohos:text_size="28fp" 
  17.         ohos:text_color="#000000" 
  18.         ohos:text_alignment="left|vertical_center" 
  19.         ohos:background_element="#78C6C5"/> 
  20.  
  21.     <Text 
  22.         ohos:id="$+id:Latitude" 
  23.         ohos:height="match_content" 
  24.         ohos:width="match_parent" 
  25.         ohos:margin="10vp" 
  26.         ohos:padding="10vp" 
  27.         ohos:text="---" 
  28.         ohos:text_size="28fp" 
  29.         ohos:text_color="#000000" 
  30.         ohos:text_alignment="left|vertical_center" 
  31.         ohos:background_element="#78C6C5"/> 
  32.  
  33.     <Text 
  34.         ohos:id="$+id:CountryName" 
  35.         ohos:height="match_content" 
  36.         ohos:width="match_parent" 
  37.         ohos:margin="10vp" 
  38.         ohos:padding="10vp" 
  39.         ohos:text="---" 
  40.         ohos:text_size="28fp" 
  41.         ohos:text_color="#000000" 
  42.         ohos:text_alignment="left|vertical_center" 
  43.         ohos:background_element="#78C6C5"/> 
  44.  
  45.     <Text 
  46.         ohos:id="$+id:PlaceName" 
  47.         ohos:height="match_content" 
  48.         ohos:width="match_parent" 
  49.         ohos:margin="10vp" 
  50.         ohos:padding="10vp" 
  51.         ohos:text="---" 
  52.         ohos:text_size="28fp" 
  53.         ohos:text_color="#000000" 
  54.         ohos:text_alignment="left|vertical_center" 
  55.         ohos:background_element="#78C6C5" 
  56.         ohos:multiple_lines="true"/> 
  57.  
  58.     <TextField 
  59.         ohos:id="$+id:tf_Distance" 
  60.         ohos:height="match_content" 
  61.         ohos:width="match_parent" 
  62.         ohos:margin="10vp" 
  63.         ohos:padding="10vp" 
  64.         ohos:hint="請輸入距離......" 
  65.         ohos:text_size="28fp" 
  66.         ohos:text_color="#000000" 
  67.         ohos:text_alignment="left|vertical_center" 
  68.         ohos:background_element="#BEBEBE"/> 
  69.  
  70.     <Text 
  71.         ohos:id="$+id:IDIDID" 
  72.         ohos:height="match_content" 
  73.         ohos:width="match_parent" 
  74.         ohos:margin="10vp" 
  75.         ohos:padding="10vp" 
  76.         ohos:text="---" 
  77.         ohos:text_size="28fp" 
  78.         ohos:text_color="#000000" 
  79.         ohos:text_alignment="left|vertical_center" 
  80.         ohos:background_element="#78C6C5" 
  81.         ohos:multiple_lines="true"/> 
  82.  
  83. </DirectionalLayout> 

3. 重寫getRange()函數。

在MainAbilitySlice.java中編寫以下代碼。

添加兩個變量Longitude_phone和Latitude_phone,并初始化為“null”,用于記錄家長端設備的經緯度信息。通過location.getLongitude()方法獲取經度信息,通過location.getLatitude()方法獲取緯度信息。

通過getText()方法獲取設置的活動距離,通過兩個位置的經緯度計算其距離,并與設置的活動距離進行比較大小。根據大小關系通過startAbility方法播放音頻,通過stopAbility方法停止播放音頻。

  1. private static String Longitude_phone = "null"
  2.    private static String Latitude_phone = "null"
  3.  
  4.    private void getLocation(){ 
  5.        writeData("Longitude", Longitude); 
  6.        writeData("Latitude", Latitude); 
  7.        writeData("PlaceName", PlaceName); 
  8.        writeData("CountryName", CountryName); 
  9.  
  10.        timer_phone = new Timer(); 
  11.        timer_phone.schedule(new TimerTask() { 
  12.            @Override 
  13.            public void run() { 
  14.                getUITaskDispatcher().asyncDispatch(new Runnable() { 
  15.                    @Override 
  16.                    public void run() { 
  17.                        getRange(); 
  18.                        getSingleLocation(); 
  19.                    } 
  20.                }); 
  21.            } 
  22.        },0,5000); 
  23.    } 
  24.  
  25.    private void getRange(){ 
  26.        TextField tf_Distance = (TextField) findComponentById(ResourceTable.Id_tf_Distance); 
  27.        Text text = (Text) findComponentById(ResourceTable.Id_IDIDID); 
  28.        double Dis; 
  29.  
  30.        if(location == null){ 
  31.            Longitude_phone = "null"
  32.            Latitude_phone = "null"
  33.            return
  34.        } 
  35.        Longitude_phone = Double.toString(location.getLongitude()); 
  36.        Latitude_phone = Double.toString(location.getLatitude()); 
  37.  
  38.        if(tf_Distance.getText().equals("")){ 
  39.            Dis = -1; 
  40.        }else { 
  41.            Dis = Double.parseDouble(tf_Distance.getText()); 
  42.        } 
  43.  
  44.        Intent serviceintent = new Intent(); 
  45.        Operation serviceOperation = new Intent.OperationBuilder() 
  46.                .withDeviceId(""
  47.                .withBundleName(getBundleName()) 
  48.                .withAbilityName(ServiceAbility.class.getName()) 
  49.                .build(); 
  50.        serviceintent.setOperation(serviceOperation); 
  51.  
  52.        if(!(Longitude.equals("null") && Latitude.equals("null") && Longitude_phone.equals("null") && Latitude_phone.equals("null"))){ 
  53.            double Lon = getRadian(Double.parseDouble(Longitude)); 
  54.            double Lat = getRadian(Double.parseDouble(Latitude)); 
  55.            double Lon_phone = getRadian(Double.parseDouble(Longitude_phone)); 
  56.            double Lat_phone = getRadian(Double.parseDouble(Latitude_phone)); 
  57.  
  58.            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))); 
  59.            distance = Double.parseDouble(Double.toString(distance).substring(0,7)); 
  60.            if(Dis != -1){ 
  61.                if(distance <= Dis){ 
  62.                    text.setText("你距離你的孩子" + distance + "米,沒有超出距離"); 
  63.                    stopAbility(serviceintent); 
  64.                }else { 
  65.                    text.setText("你距離你的孩子" + distance + "米,已經超出距離"); 
  66.                    startAbility(serviceintent); 
  67.                } 
  68.            }else { 
  69.                text.setText("你距離你的孩子" + distance + "米,你沒有設定距離"); 
  70.                stopAbility(serviceintent); 
  71.            } 
  72.        }else { 
  73.            text.setText("沒有獲取位置"); 
  74.            stopAbility(serviceintent); 
  75.        } 
  76.    } 
  77.  
  78.    private double getRadian(double degree){//轉化為弧度制 
  79.        return degree / 180 * Math.PI; 
  80.    } 

文章相關附件可以點擊下面的原文鏈接前往下載

ChildrenLocation2.zip

ChildrenLocation3.zip

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2010-05-11 10:15:19

Unix awk

2012-09-26 09:19:26

OfficeOutlook 201

2014-11-05 16:37:54

2015-08-19 16:08:39

搜狗糖貓兒童手表

2010-01-07 09:10:06

2022-04-14 11:35:01

HarmonyOS手表Demo操作系統

2022-04-13 11:49:37

HarmonyOS操作系統鴻蒙

2015-06-16 12:06:01

校園做公益

2013-11-05 10:40:06

瀏覽器Pale MoonFirefox

2017-08-28 15:39:53

物聯網

2017-08-29 12:07:25

互聯網

2014-12-04 09:47:59

2015-04-13 10:13:29

2021-01-31 09:52:49

SSH監控網絡攻擊

2011-12-07 21:10:37

iOS

2014-09-29 16:39:43

兒童智能手環智能設備安全
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久大陆 | 亚洲久久在线 | 亚洲视频在线观看 | 欧美性高潮 | 精品日韩一区 | 丝袜美腿一区二区三区 | 欧美日韩一区二区在线观看 | 免费黄色日本 | 97精品视频在线观看 | 国产一区二区三区四区三区四 | 久久综合久久综合久久综合 | 亚洲国产精品一区二区三区 | 欧美一区二区三区一在线观看 | 精品久久久久久 | 久久久久久久久久久一区二区 | 成人性视频免费网站 | 国产免费一区 | 日韩成人精品一区 | 在线观看免费毛片 | 久久综合色综合 | a国产一区二区免费入口 | 黄色大全免费看 | 亚洲成人免费 | 欧美精选一区二区 | 成人不卡视频 | 亚洲一区二区中文字幕 | 国产黄色网址在线观看 | 国产福利在线 | 国产精品久久久久久久久免费桃花 | 精品久久亚洲 | 国产精品亚洲成在人线 | 天堂久久一区 | 日韩中文字幕网 | 日本精a在线观看 | h视频在线观看免费 | 中文字幕国产一区 | 免费一区| 欧美激情在线观看一区二区三区 | 成人免费网站在线 | 欧美日本韩国一区二区 | 欧美视频福利 |