地理围栏API服务开发
生活随笔
收集整理的這篇文章主要介紹了
地理围栏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文件中注冊廣播接收器。
- <receiver
-
android:name=".geofence.GeoFenceBroadcastReceiver" -
android:exported="true"> -
<intent-filter> -
<action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" /> -
</intent-filter>
創建地理圍欄服務客戶端
在Activity的OnCreate()方法中創建GeofenceService實例,并使用該實例調用與geofence相關的API接口。
- private GeofenceService geofenceService;
- private ArrayList idList;
- private ArrayList geofenceList;
- private String TAG;
- private PendingIntent pendingIntent;
- protected void onCreate(Bundle savedInstanceState) {
-
// 創建一個新的GeofenceService實例 -
geofenceService = LocationServices.getGeofenceService(this); -
// 獲取PendingIntent對象 -
pendingIntent = getPendingIntent(); -
idList = new ArrayList<String>(); -
geofenceList = new ArrayList<Geofence>(); -
TAG = "geoFence"; - }
創建并添加地理圍欄
可以先創建地理圍欄實例,并構建添加地理圍欄的請求。在發送請求之后,會通過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 { -
Log.w(TAG, "add geofence failed : " + task.getException().getMessage()); -
} -
} -
}); - }
- 移除地理圍欄。除了通過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 { -
Log.w(TAG, "delete geofence with ID failed "); -
} -
} -
}); - }
- 地理圍欄觸發信息處理。
當檢測到用戶觸發圍欄事件時,會通過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"; -
if (ACTION_PROCESS_LOCATION.equals(action)) { -
// 從intent中解析出GeofenceData對象 -
GeofenceData geofenceData = GeofenceData.getDataFromIntent(intent); -
if (geofenceData != null) { -
// 獲取錯誤碼 -
int errorCode = geofenceData.getErrorCode(); -
// 獲取地理圍欄觸發類型 -
int conversion = geofenceData.getConversion(); -
// 獲取觸發的地理圍欄信息 -
List<Geofence> list = geofenceData.getConvertingGeofenceList(); -
// 獲取觸發時的位置信息 -
Location mLocation = geofenceData.getConvertingLocation(); -
// 是否是正常觸發圍欄事件,返回false時表示出現錯誤 -
boolean status = geofenceData.isSuccess(); -
sb.append("errorcode: " + errorCode + next); -
sb.append("conversion: " + conversion + next); -
for (int i = 0; i < list.size(); i++){ -
sb.append("geoFence id :" + list.get(i).getUniqueId() + next); -
} -
sb.append("location is :" + mLocation.getLongitude() + " " + mLocation.getLatitude() + next); -
sb.append("is successful :" + status); -
Log.i(TAG,sb.toString()); -
} -
} -
} -
} - }
總結
以上是生活随笔為你收集整理的地理围栏API服务开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 活动识别API服务开发
- 下一篇: TensorFlow XLA加速编译器