日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

地理围栏API服务开发

發布時間:2023/11/28 生活经验 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 地理围栏API服务开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

地理圍欄API服務開發
要使用華為地理圍欄服務API,需要確保設備已經下載并安裝了HMS Core(APK),并將Location Kit的SDK集成到項目中。
指定應用權限
? 如果需要使用地理圍欄服務API,需要在“AndroidManifest.xml”文件中申請ACCESS_FINE_LOCATION權限和ACCESS_COARSE_LOCATION權限:
a.
b.
? 在Android Q版本中,需要在“AndroidManifest.xml”文件中申請ACCESS_BACKGROUND_LOCATION權限:
.
說明
以上地理圍欄相關權限屬于危險權限,使用時需要動態申請。
注冊靜態廣播
地理圍欄觸發信息通過廣播接收,需要在Manifest文件中注冊廣播接收器。

  1. <receiver
  2.  android:name=".geofence.GeoFenceBroadcastReceiver"
    
  3.  android:exported="true">
    
  4.  <intent-filter>
    
  5.      <action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" />
    
  6.  </intent-filter>
    

創建地理圍欄服務客戶端
在Activity的OnCreate()方法中創建GeofenceService實例,并使用該實例調用與geofence相關的API接口。

  1. private GeofenceService geofenceService;
  2. private ArrayList idList;
  3. private ArrayList geofenceList;
  4. private String TAG;
  5. private PendingIntent pendingIntent;
  6. protected void onCreate(Bundle savedInstanceState) {
  7.  // 創建一個新的GeofenceService實例
    
  8.  geofenceService = LocationServices.getGeofenceService(this);
    
  9.  // 獲取PendingIntent對象
    
  10. pendingIntent = getPendingIntent();
    
  11. idList = new ArrayList<String>();
    
  12. geofenceList = new ArrayList<Geofence>();
    
  13. TAG = "geoFence";
    
  14. }
    創建并添加地理圍欄
    可以先創建地理圍欄實例,并構建添加地理圍欄的請求。在發送請求之后,會通過Task通知是否添加成功。
  15. 創建地理圍欄實例。
  16. geofenceList.add(new Geofence.Builder()
  17.      .setUniqueId("mGeofence")    
    
  18.      .setValidContinueTime(10000)    
    
  19.      // 傳入經緯度信息,圓形地理圍欄半徑(單位:米)
    
  20.      .setRoundArea(latitude, longitude, radius)    
    
  21.      // 進入或退出圍欄時觸發回調
    
  22.      .setConversions(Geofence.ENTER_GEOFENCE_CONVERSION | Geofence.EXIT_GEOFENCE_CONVERSION)    
    
  23.      .build());
    
  24. idList.add(“mGeofence”);
  25. 創建添加地理圍欄的請求。
  26. private GeofenceRequest getAddGeofenceRequest() {
  27.  GeofenceRequest.Builder builder = new GeofenceRequest.Builder();    
    
  28.  // 當用戶在圍欄中時,添加圍欄后立即觸發回調
    
  29.  builder.setInitConversions(GeofenceRequest.ENTER_INIT_CONVERSION);    
    
  30.  builder.createGeofenceList(geofenceList);    
    
  31.  return builder.build();
    
  32. }
  33. 動態注冊GeoFenceBroadcastReceiver廣播接收器。
  34. // 通過PendingIntent動態注冊GeoFenceBroadcastReceiver廣播接收器,當觸發圍欄的時候,會通過廣播通知。
  35. private PendingIntent getPendingIntent() {
  36.  Intent intent = new Intent(this, GeoFenceBroadcastReceiver.class);
    
  37.  intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);
    
  38.  return PendingIntent.getBroadcast(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
  39. }
  40. 發送添加地理圍欄請求。
  41. public void requestGeoFenceWithNewIntent() {
  42.  // 通過傳入PendingIntent的方式添加地理圍欄,并處理地理圍欄添加行為的響應數據
    
  43.  geofenceService.createGeofenceList(getAddGeofenceRequest(), pendingIntent)
    
  44.      .addOnCompleteListener(new OnCompleteListener<Void>() {
    
  45.          @Override
    
  46.          public void onComplete(Task<Void> task) {
    
  47.              if (task.isSuccessful()) {
    
  48.                  Log.i(TAG, "add geofence success!");
    
  49.              } else {
    
  50.                 Log.w(TAG, "add geofence failed : " + task.getException().getMessage());
    
  51.             }
    
  52.         }
    
  53.     });
    
  54. }
  55. 移除地理圍欄。除了通過id移除地理圍欄,還可以通過PendingIntent進行移除。
  56. public void removeWithID() {
  57.  // 通過id移除地理圍欄,并處理地理圍欄移除行為的響應數據
    
  58.  geofenceService.deleteGeofenceList(idList)
    
  59.      .addOnCompleteListener(new OnCompleteListener<Void>() {
    
  60.          @Override
    
  61.          public void onComplete(Task<Void> task) {
    
  62.              if (task.isSuccessful()) {
    
  63.                  Log.i(TAG, "delete geofence with ID success!");
    
  64.              } else {
    
  65.                 Log.w(TAG, "delete geofence with ID failed ");
    
  66.             }
    
  67.         }
    
  68.     });
    
  69. }
  70. 地理圍欄觸發信息處理。
    當檢測到用戶觸發圍欄事件時,會通過PendingIntent發送廣播通知用戶。
  71. // 地理圍欄服務廣播接收器
  72. public class GeoFenceBroadcastReceiver extends BroadcastReceiver {
  73.  public static final String ACTION_PROCESS_LOCATION = "com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION";
    
  74.  @Override
    
  75.  public void onReceive(Context context, Intent intent) {
    
  76.      if (intent != null) {
    
  77.          final String action = intent.getAction();
    
  78.          StringBuilder sb = new StringBuilder();
    
  79.          String next = "\n";
    
  80.         if (ACTION_PROCESS_LOCATION.equals(action)) {
    
  81.             // 從intent中解析出GeofenceData對象
    
  82.             GeofenceData geofenceData = GeofenceData.getDataFromIntent(intent);
    
  83.             if (geofenceData != null) {
    
  84.                // 獲取錯誤碼
    
  85.                int errorCode = geofenceData.getErrorCode();
    
  86.                // 獲取地理圍欄觸發類型
    
  87.                int conversion = geofenceData.getConversion();
    
  88.                // 獲取觸發的地理圍欄信息
    
  89.                List<Geofence> list = geofenceData.getConvertingGeofenceList();
    
  90.                // 獲取觸發時的位置信息
    
  91.                Location mLocation = geofenceData.getConvertingLocation();
    
  92.                // 是否是正常觸發圍欄事件,返回false時表示出現錯誤
    
  93.                boolean status = geofenceData.isSuccess();
    
  94.                sb.append("errorcode: " + errorCode + next);
    
  95.                sb.append("conversion: " + conversion + next);
    
  96.                for (int i = 0; i < list.size(); i++){
    
  97.                    sb.append("geoFence id :" + list.get(i).getUniqueId() + next);
    
  98.                }
    
  99.                sb.append("location is :" + mLocation.getLongitude() + " " + mLocation.getLatitude() + next);
    
  100.                sb.append("is successful :" + status);
    
  101.                Log.i(TAG,sb.toString());
    
  102.             }
    
  103.         }
    
  104.     }
    
  105. }
    
  106. }

總結

以上是生活随笔為你收集整理的地理围栏API服务开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。