地理围栏API服务开发

地理围栏API服务开发

要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中。

指定应用权限

  • 如果需要使用地理围栏服务API,需要在“AndroidManifest.xml”文件中申请ACCESS_FINE_LOCATION权限和ACCESS_COARSE_LOCATION权限:
  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  2. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  • 在Android Q版本中,需要在“AndroidManifest.xml”文件中申请ACCESS_BACKGROUND_LOCATION权限:

 .          <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

说明

以上地理围栏相关权限属于危险权限,使用时需要动态申请。

注册静态广播

地理围栏触发信息通过广播接收,需要在Manifest文件中注册广播接收器。

  1. <!--注册地理围栏服务广播接收器-->
  2. <receiver
  3.     android:name=".geofence.GeoFenceBroadcastReceiver"
  4.     android:exported="true">
  5.     <intent-filter>
  6.         <action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" />
  7.     </intent-filter>
  8. </receiver>

创建地理围栏服务客户端

在Activity的OnCreate()方法中创建GeofenceService实例,并使用该实例调用与geofence相关的API接口。

  1. private GeofenceService geofenceService;
  2. private ArrayList<String> idList;
  3. private ArrayList<Geofence> 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. 10.     pendingIntent = getPendingIntent();
  11. 11.     idList = new ArrayList<String>();
  12. 12.     geofenceList = new ArrayList<Geofence>();
  13. 13.     TAG = "geoFence";

14. }

创建并添加地理围栏

可以先创建地理围栏实例,并构建添加地理围栏的请求。在发送请求之后,会通过Task通知是否添加成功。

  1. 创建地理围栏实例。
  2. geofenceList.add(new Geofence.Builder()   
  3.         .setUniqueId("mGeofence")   
  4.         .setValidContinueTime(10000)   
  5.         // 传入经纬度信息,圆形地理围栏半径(单位:米)
  6.         .setRoundArea(latitude, longitude, radius)   
  7.         // 进入或退出围栏时触发回调
  8.         .setConversions(Geofence.ENTER_GEOFENCE_CONVERSION | Geofence.EXIT_GEOFENCE_CONVERSION)   
  9.         .build());
  10. idList.add("mGeofence");
  11. 创建添加地理围栏的请求。
  12. private GeofenceRequest getAddGeofenceRequest() {   
  13.     GeofenceRequest.Builder builder = new GeofenceRequest.Builder();   
  14.     // 当用户在围栏中时,添加围栏后立即触发回调
  15.     builder.setInitConversions(GeofenceRequest.ENTER_INIT_CONVERSION);   
  16.     builder.createGeofenceList(geofenceList);   
  17.     return builder.build();
  18. }
  19. 动态注册GeoFenceBroadcastReceiver广播接收器。
  20. // 通过PendingIntent动态注册GeoFenceBroadcastReceiver广播接收器,当触发围栏的时候,会通过广播通知。
  21. private PendingIntent getPendingIntent() {
  22.     Intent intent = new Intent(this, GeoFenceBroadcastReceiver.class);
  23.     intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);
  24.     return PendingIntent.getBroadcast(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  25. }
  26. 发送添加地理围栏请求。
  27. public void requestGeoFenceWithNewIntent() {
  28.     // 通过传入PendingIntent的方式添加地理围栏,并处理地理围栏添加行为的响应数据
  29.     geofenceService.createGeofenceList(getAddGeofenceRequest(), pendingIntent)
  30.         .addOnCompleteListener(new OnCompleteListener<Void>() {
  31.             @Override
  32.             public void onComplete(Task<Void> task) {
  33.                 if (task.isSuccessful()) {
  34.                     Log.i(TAG, "add geofence success!");
  35.                 } else {
  36. 10.                     Log.w(TAG, "add geofence failed : " + task.getException().getMessage());
  37. 11.                 }
  38. 12.             }
  39. 13.         });

14. }

  1. 移除地理围栏。除了通过id移除地理围栏,还可以通过PendingIntent进行移除。
  2. public void removeWithID() {
  3.     // 通过id移除地理围栏,并处理地理围栏移除行为的响应数据
  4.     geofenceService.deleteGeofenceList(idList)
  5.         .addOnCompleteListener(new OnCompleteListener<Void>() {
  6.             @Override
  7.             public void onComplete(Task<Void> task) {
  8.                 if (task.isSuccessful()) {
  9.                     Log.i(TAG, "delete geofence with ID success!");
  10.                 } else {
  11. 10.                     Log.w(TAG, "delete geofence with ID failed ");
  12. 11.                 }
  13. 12.             }
  14. 13.         });

14. }

  1. 地理围栏触发信息处理。

当检测到用户触发围栏事件时,会通过PendingIntent发送广播通知用户。

  1. // 地理围栏服务广播接收器
  2. public class GeoFenceBroadcastReceiver extends BroadcastReceiver {
  3.     public static final String ACTION_PROCESS_LOCATION = "com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION";
  4.     @Override
  5.     public void onReceive(Context context, Intent intent) {
  6.         if (intent != null) {
  7.             final String action = intent.getAction();
  8.             StringBuilder sb = new StringBuilder();
  9.             String next = "\n";
  10. 10.             if (ACTION_PROCESS_LOCATION.equals(action)) {
  11. 11.                 // 从intent中解析出GeofenceData对象
  12. 12.                 GeofenceData geofenceData = GeofenceData.getDataFromIntent(intent);
  13. 13.                 if (geofenceData != null) {
  14. 14.                    // 获取错误码
  15. 15.                    int errorCode = geofenceData.getErrorCode();
  16. 16.                    // 获取地理围栏触发类型
  17. 17.                    int conversion = geofenceData.getConversion();
  18. 18.                    // 获取触发的地理围栏信息
  19. 19.                    List<Geofence> list = geofenceData.getConvertingGeofenceList();
  20. 20.                    // 获取触发时的位置信息
  21. 21.                    Location mLocation = geofenceData.getConvertingLocation();
  22. 22.                    // 是否是正常触发围栏事件,返回false时表示出现错误
  23. 23.                    boolean status = geofenceData.isSuccess();
  24. 24.                    sb.append("errorcode: " + errorCode + next);
  25. 25.                    sb.append("conversion: " + conversion + next);
  26. 26.                    for (int i = 0; i < list.size(); i++){
  27. 27.                        sb.append("geoFence id :" + list.get(i).getUniqueId() + next);
  28. 28.                    }
  29. 29.                    sb.append("location is :" + mLocation.getLongitude() + " " + mLocation.getLatitude() + next);
  30. 30.                    sb.append("is successful :" + status);
  31. 31.                    Log.i(TAG,sb.toString());
  32. 32.                 }
  33. 33.             }
  34. 34.         }
  35. 35.     }

36. }

 

posted @ 2021-01-29 07:22  吴建明wujianming  阅读(311)  评论(0编辑  收藏  举报