日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
HarmonyOS使用Java獲取位置信息

想了解更多內(nèi)容,請訪問:

創(chuàng)新互聯(lián)公司是一家業(yè)務(wù)范圍包括IDC托管業(yè)務(wù),網(wǎng)站空間、主機(jī)租用、主機(jī)托管,四川、重慶、廣東電信服務(wù)器租用,遂寧托管服務(wù)器,成都網(wǎng)通服務(wù)器托管,成都服務(wù)器租用,業(yè)務(wù)范圍遍及中國大陸、港澳臺以及歐美等多個國家及地區(qū)的互聯(lián)網(wǎng)數(shù)據(jù)服務(wù)公司。

和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.

前言

隨著科技時代的發(fā)展,生活中每個人都離不開手機(jī),當(dāng)我們?nèi)ヒ粋€陌生的地方,不認(rèn)識路怎么辦,大家都會想到手機(jī)里的百度地圖、高德地圖等等。生活中我們不想做飯也不想出門的時候,我們會想到美團(tuán),那么美團(tuán)APP怎么知道你的位置信息呢?當(dāng)你進(jìn)入app時,頁面中就會提示你是否允許獲取位置信息,當(dāng)你點擊允許時,就會把位置信息展示在頁面中。今天我們就來講解一下HarmonyOS 如何使用Java UI框架獲取手機(jī)位置權(quán)限并拿到位置信息。

使用DevEco Studio 創(chuàng)建空項目,選擇java模板

File => New Project

編寫基礎(chǔ)布局

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:alignment="top|center" 
  6.     ohos:orientation="vertical"> 
  7.  
  8.     
  9.         ohos:id="$+id:T_Longitude" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_parent" 
  12.         ohos:background_element="green" 
  13.         ohos:margin="10vp" 
  14.         ohos:padding="10vp" 
  15.         ohos:text="--" 
  16.         ohos:text_alignment="left|vertical_center" 
  17.         ohos:text_size="28fp"/> 
  18.     
  19.         ohos:id="$+id:T_Latitude" 
  20.         ohos:height="match_content" 
  21.         ohos:width="match_parent" 
  22.         ohos:margin="10vp" 
  23.         ohos:padding="10vp" 
  24.         ohos:background_element="green" 
  25.         ohos:text="--" 
  26.         ohos:text_alignment="left|vertical_center" 
  27.         ohos:text_size="28fp"/> 
  28.     
  29.         ohos:id="$+id:T_country" 
  30.         ohos:height="match_content" 
  31.         ohos:width="match_parent" 
  32.         ohos:margin="10vp" 
  33.         ohos:padding="10vp" 
  34.         ohos:background_element="green" 
  35.         ohos:text="--" 
  36.         ohos:text_alignment="left|vertical_center" 
  37.         ohos:text_size="28fp"/> 
  38.     
  39.         ohos:id="$+id:T_PlaceName" 
  40.         ohos:height="match_content" 
  41.         ohos:width="match_parent" 
  42.         ohos:margin="10vp" 
  43.         ohos:multiple_lines="true" 
  44.         ohos:padding="10vp" 
  45.         ohos:background_element="green" 
  46.         ohos:text="--" 
  47.         ohos:text_alignment="left|vertical_center" 
  48.         ohos:text_size="28fp"/> 
  49.     
  50.         ohos:id="$+id:btn_location" 
  51.         ohos:height="match_content" 
  52.         ohos:width="match_content" 
  53.         ohos:text="點擊獲取定位" 
  54.         ohos:padding="10vp" 
  55.         ohos:background_element="green" 
  56.         ohos:text_alignment="vertical_center" 
  57.         ohos:text_size="28fp"/> 
  58.  

日志工具類 LogUtil

寫好布局后,我們先準(zhǔn)備一個日志工具類,用于打印日志。

 
 
 
 
  1. /** 
  2.  *日志工具類 
  3.  */ 
  4. public class LogUtil { 
  5.    static HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP,233,"LOCATION_TAG"); 
  6.     public static void info(String content){ 
  7.         HiLog.info(hiLogLabel,content); 
  8.  
  9.     } public static void error(String content){ 
  10.         HiLog.info(hiLogLabel,content); 
  11.  
  12.     } public static void debug(String content){ 
  13.         HiLog.info(hiLogLabel,content); 
  14.     } 

配置位置權(quán)限 config.js

位置信息屬于敏感信息,使用之前我們需要申請權(quán)限

 
 
 
 
  1. "reqPermissions": [ 
  2.      { 
  3.        "name": "ohos.permission.LOCATION", 
  4.        "reason": "", 
  5.        "usedScene": { 
  6.          "ability": ["com.example.location.MainAbility"], 
  7.          "when": "inuse" 
  8.        } 
  9.      } 
  10.    ] 

MainAbility 中獲取位置信息

 
 
 
 
  1. public class MainAbility extends Ability { 
  2.     Locator locator; 
  3.     MyLocatorCallback locatorCallback; 
  4.     RequestParam requestParam; 
  5.     final int REQUEST_LOCATION_CODE = 12; // 定義常量 用來表示請求碼 
  6.     // 創(chuàng)建一個靜態(tài)成員變量 數(shù)據(jù)共享 
  7.     public static Location location = null; 
  8.  
  9.     @Override 
  10.     public void onStart(Intent intent) { 
  11.         super.onStart(intent); 
  12.         super.setMainRoute(MainAbilitySlice.class.getName()); 
  13.         // 動態(tài)判斷權(quán)限 
  14.         if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED) { 
  15.             // 應(yīng)用未被授予權(quán)限 
  16.             if (canRequestPermission("ohos.permission.LOCATION")) { 
  17.                 // 是否可以申請彈框授權(quán)(首次申請或者用戶未選擇禁止且不再提示) 
  18.                 requestPermissionsFromUser(new String[]{"ohos.permission.LOCATION"}, REQUEST_LOCATION_CODE); 
  19.                 LogUtil.info("canRequestPermission() running"); 
  20.             } else { 
  21.                 // 顯示應(yīng)用需要權(quán)限的理由,提示用戶進(jìn)入設(shè)置授權(quán) 
  22.                 LogUtil.info("顯示應(yīng)用需要權(quán)限的理由"); 
  23.             } 
  24.         } else { 
  25.             initLocator(); 
  26.             // 權(quán)限已被授予 
  27.             LogUtil.info("權(quán)限已被授予,直接啟動服務(wù)"); 
  28.         } 
  29.  
  30.     } 
  31.  
  32.     @Override // 權(quán)限操作調(diào)用的方法 
  33.     public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { 
  34.         super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults); 
  35.         switch (requestCode) { 
  36.             case REQUEST_LOCATION_CODE: { 
  37.                 // 匹配requestPermissions的requestCode 
  38.                 if (grantResults.length > 0 
  39.                         && grantResults[0] == IBundleManager.PERMISSION_GRANTED) { 
  40.                     LogUtil.info("onRequestPermissionsFromUserResult權(quán)限被授予"); 
  41.                     // 權(quán)限被授予 
  42.                     // 注意:因時間差導(dǎo)致接口權(quán)限檢查時有無權(quán)限,所以對那些因無權(quán)限而拋異常的接口進(jìn)行異常捕獲處理 
  43.                     initLocator(); 
  44.                 } else { 
  45.                     // 權(quán)限被拒絕 
  46.                     LogUtil.info("onRequestPermissionsFromUserResult權(quán)限被拒絕"); 
  47.                 } 
  48.                 return; 
  49.             } 
  50.         } 
  51.     } 
  52.  
  53.     private void initLocator() { 
  54.         locator = new Locator(this); // 創(chuàng)建local對象 
  55.         // 實例化RequestParam對象,用于告知系統(tǒng)該向應(yīng)用提供何種類型的位置服務(wù),以及位置結(jié)果上報的頻率。 
  56.         requestParam = new RequestParam(RequestParam.SCENE_NAVIGATION); 
  57.         // 實現(xiàn) locatorCallback 接口對象 
  58.         locatorCallback = new MyLocatorCallback(); 
  59.         // locator.requestOnce(requestParam, locatorCallback); // 請求一次 
  60.         locator.startLocating(requestParam, locatorCallback); // 多次請求 直接啟動服務(wù) 
  61.     } 
  62.  
  63.     @Override 
  64.     protected void onStop() { 
  65.         super.onStop(); 
  66.         locator.stopLocating(locatorCallback); // 程序結(jié)束 停止服務(wù) 
  67.     } 
  68.  
  69.     // 創(chuàng)建MyLocatorCallback類,實現(xiàn)LocatorCallback接口,用于執(zhí)行定位過程的回調(diào)方法 
  70.     public class MyLocatorCallback implements LocatorCallback { 
  71.         @Override // 獲取定位結(jié)果 
  72.         public void onLocationReport(Location location) { // 使用靜態(tài)成員變量 進(jìn)行分享 
  73.             LogUtil.info("onLocationReport:" + location.getLongitude() + "," + location.getLatitude()); 
  74.             if (location != null) { 
  75.                 MainAbility.location = location; 
  76.             } 
  77.         } 
  78.  
  79.         @Override // 獲取定位過程中的狀態(tài)信息 
  80.         public void onStatusChanged(int type) { 
  81.             LogUtil.info("onStatusChanged:" + type); 
  82.  
  83.         } 
  84.  
  85.         @Override // 獲取定位過程中的錯誤信息 
  86.         public void onErrorReport(int type) { 
  87.             LogUtil.info("onErrorReport:" + type); 
  88.         } 
  89.     } 

更新視圖

通過以上操作,我們拿到了位置信息,接下來我們需要把位置信息渲染到視圖中

 
 
 
 
  1. public class MainAbilitySlice extends AbilitySlice { 
  2.     Text t_Longitude; 
  3.     Text t_Latitude; 
  4.     Text t_country; 
  5.     Text t_PlaceName; 
  6.  
  7.     @Override 
  8.     public void onStart(Intent intent) { 
  9.         super.onStart(intent); 
  10.         super.setUIContent(ResourceTable.Layout_ability_main); 
  11.         // 獲取UI布局中的id,用于數(shù)據(jù)綁定 
  12.         t_Longitude = (Text) findComponentById(ResourceTable.Id_T_Longitude); 
  13.         t_Latitude = (Text) findComponentById(ResourceTable.Id_T_Latitude); 
  14.         t_PlaceName = (Text) findComponentById(ResourceTable.Id_T_PlaceName); 
  15.         t_country = (Text) findComponentById(ResourceTable.Id_T_country); 
  16.  
  17.         findComponentById(ResourceTable.Id_btn_location).setClickedListener(new Component.ClickedListener() { 
  18.             @Override 
  19.             public void onClick(Component component) { 
  20.                 loadLocation(); 
  21.             } 
  22.         }); 
  23.     } 
  24.  
  25.     private void loadLocation(){ 
  26.         // 在UI線程中更新組件 
  27.         getUITaskDispatcher().asyncDispatch(new Runnable() { 
  28.             @Override 
  29.             public void run() { 
  30.                 // 是UI中的操作 
  31.                 if(location == null)return; 
  32.                 t_Longitude.setText("經(jīng)度:"+location.getLongitude() +""); 
  33.                 t_Latitude.setText("緯度:"+location.getLatitude() +""); 
  34.                 // (逆)地理編碼轉(zhuǎn)換 
  35.                 GeoConvert geoConvert = new GeoConvert(); 
  36.                 try { 
  37.                     List list = geoConvert.getAddressFromLocation(location.getLatitude(),location.getLongitude(),1); 
  38.                     GeoAddress geoAddress = list.get(0); 
  39.                     if(geoAddress == null) return; 
  40.                     t_PlaceName.setText("位置:"+geoAddress.getPlaceName()+""); 
  41.                     t_country.setText("國家:"+geoAddress.getCountryName()+""); 
  42.                 } catch (IOException e) { 
  43.                     e.printStackTrace(); 
  44.                 } 
  45.             } 
  46.         }); 
  47.     } 
  48.  
  49.     @Override 
  50.     public void onActive() { 
  51.         super.onActive(); 
  52.     } 
  53.  
  54.     @Override 
  55.     public void onForeground(Intent intent) { 
  56.         super.onForeground(intent); 
  57.     } 

實現(xiàn)效果圖如下:

想了解更多內(nèi)容,請訪問:

和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.


文章標(biāo)題:HarmonyOS使用Java獲取位置信息
本文路徑:http://www.dlmjj.cn/article/dppgesd.html