地理围栏API服务开发
要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中。
指定应用权限
- 如果需要使用地理围栏服务API,需要在“AndroidManifest.xml”文件中申请ACCESS_FINE_LOCATION权限和ACCESS_COARSE_LOCATION权限:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <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文件中注册广播接收器。
- <!--注册地理围栏服务广播接收器-->
- <receiver
- android:name=".geofence.GeoFenceBroadcastReceiver"
- android:exported="true">
- <intent-filter>
- <action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" />
- </intent-filter>
- </receiver>
创建地理围栏服务客户端
在Activity的OnCreate()方法中创建GeofenceService实例,并使用该实例调用与geofence相关的API接口。
- private GeofenceService geofenceService;
- private ArrayList<String> idList;
- private ArrayList<Geofence> geofenceList;
- private String TAG;
- private PendingIntent pendingIntent;
- protected void onCreate(Bundle savedInstanceState) {
- // 创建一个新的GeofenceService实例
- geofenceService = LocationServices.getGeofenceService(this);
- // 获取PendingIntent对象
- 10. pendingIntent = getPendingIntent();
- 11. idList = new ArrayList<String>();
- 12. geofenceList = new ArrayList<Geofence>();
- 13. TAG = "geoFence";
14. }
创建并添加地理围栏
可以先创建地理围栏实例,并构建添加地理围栏的请求。在发送请求之后,会通过Task通知是否添加成功。
- 创建地理围栏实例。
- geofenceList.add(new Geofence.Builder()
- .setUniqueId("mGeofence")
- .setValidContinueTime(10000)
- // 传入经纬度信息,圆形地理围栏半径(单位:米)
- .setRoundArea(latitude, longitude, radius)
- // 进入或退出围栏时触发回调
- .setConversions(Geofence.ENTER_GEOFENCE_CONVERSION | Geofence.EXIT_GEOFENCE_CONVERSION)
- .build());
- idList.add("mGeofence");
- 创建添加地理围栏的请求。
- private GeofenceRequest getAddGeofenceRequest() {
- GeofenceRequest.Builder builder = new GeofenceRequest.Builder();
- // 当用户在围栏中时,添加围栏后立即触发回调
- builder.setInitConversions(GeofenceRequest.ENTER_INIT_CONVERSION);
- builder.createGeofenceList(geofenceList);
- return builder.build();
- }
- 动态注册GeoFenceBroadcastReceiver广播接收器。
- // 通过PendingIntent动态注册GeoFenceBroadcastReceiver广播接收器,当触发围栏的时候,会通过广播通知。
- private PendingIntent getPendingIntent() {
- Intent intent = new Intent(this, GeoFenceBroadcastReceiver.class);
- intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);
- return PendingIntent.getBroadcast(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- }
- 发送添加地理围栏请求。
- public void requestGeoFenceWithNewIntent() {
- // 通过传入PendingIntent的方式添加地理围栏,并处理地理围栏添加行为的响应数据
- geofenceService.createGeofenceList(getAddGeofenceRequest(), pendingIntent)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "add geofence success!");
- } else {
- 10. Log.w(TAG, "add geofence failed : " + task.getException().getMessage());
- 11. }
- 12. }
- 13. });
14. }
- 移除地理围栏。除了通过id移除地理围栏,还可以通过PendingIntent进行移除。
- public void removeWithID() {
- // 通过id移除地理围栏,并处理地理围栏移除行为的响应数据
- geofenceService.deleteGeofenceList(idList)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "delete geofence with ID success!");
- } else {
- 10. Log.w(TAG, "delete geofence with ID failed ");
- 11. }
- 12. }
- 13. });
14. }
- 地理围栏触发信息处理。
当检测到用户触发围栏事件时,会通过PendingIntent发送广播通知用户。
- // 地理围栏服务广播接收器
- public class GeoFenceBroadcastReceiver extends BroadcastReceiver {
- public static final String ACTION_PROCESS_LOCATION = "com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION";
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent != null) {
- final String action = intent.getAction();
- StringBuilder sb = new StringBuilder();
- String next = "\n";
- 10. if (ACTION_PROCESS_LOCATION.equals(action)) {
- 11. // 从intent中解析出GeofenceData对象
- 12. GeofenceData geofenceData = GeofenceData.getDataFromIntent(intent);
- 13. if (geofenceData != null) {
- 14. // 获取错误码
- 15. int errorCode = geofenceData.getErrorCode();
- 16. // 获取地理围栏触发类型
- 17. int conversion = geofenceData.getConversion();
- 18. // 获取触发的地理围栏信息
- 19. List<Geofence> list = geofenceData.getConvertingGeofenceList();
- 20. // 获取触发时的位置信息
- 21. Location mLocation = geofenceData.getConvertingLocation();
- 22. // 是否是正常触发围栏事件,返回false时表示出现错误
- 23. boolean status = geofenceData.isSuccess();
- 24. sb.append("errorcode: " + errorCode + next);
- 25. sb.append("conversion: " + conversion + next);
- 26. for (int i = 0; i < list.size(); i++){
- 27. sb.append("geoFence id :" + list.get(i).getUniqueId() + next);
- 28. }
- 29. sb.append("location is :" + mLocation.getLongitude() + " " + mLocation.getLatitude() + next);
- 30. sb.append("is successful :" + status);
- 31. Log.i(TAG,sb.toString());
- 32. }
- 33. }
- 34. }
- 35. }
36. }