Before calling the location awareness capability, assign required permissions in the manifest file.
- <!-- Location permission, which is sensitive. After the permission is declared, you need to dynamically apply for it in the code. -->
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <!-- If your app uses the barrier capability and runs on Android 10 or later, you need to add the background location access permission, which is also a sensitive permission. -->
- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
To use the location awareness capability, you need to import the public capability classes of HUAWEI Awareness Kit, and also the location-related classes.
- import com.huawei.hmf.tasks.OnFailureListener;
- import com.huawei.hmf.tasks.OnSuccessListener;
- import com.huawei.hms.kit.awareness.Awareness;
- // Import the location capture-related classes.
- import android.location.Location;
- import com.huawei.hms.kit.awareness.capture.LocationResponse;
- // Import the geofence-related classes.
- import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
- import com.huawei.hms.kit.awareness.barrier.BarrierStatus;
- import com.huawei.hms.kit.awareness.barrier.LocationBarrier;
- import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;
1. Obtain the Capture Client object of HUAWEI Awareness Kit.
2. Call the location query capability API through the Capture Client object to obtain the user's context.
3. Enable your app to listen for the result returned by HUAWEI Awareness Kit for further processing.
- Awareness.getCaptureClient(this).getLocation()
- .addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
- @Override
- public void onSuccess(LocationResponse locationResponse) {
- Location location = locationResponse.getLocation();
- Log.i(TAG, "Longitude:" + location.getLongitude()
- + ",Latitude:" + location.getLatitude());
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- Log.e(TAG, "get location failed", e);
- }
- });
The following example illustrates how to develop a barrier triggered by the enter condition. That is, the barrier will be triggered when a user enters a specified district.
1. Define the barrier.
- double latitude = 22.4943;
- double longitude = 113.7436;
- double radius = 200;
- AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);
2. Define PendingIntent that will be triggered upon a barrier status change, for example, to send a broadcast, and create a broadcast receiver to receive the broadcast.
- final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "LOCATION_BARRIER_RECEIVER_ACTION";
- Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- LocationBarrierReceiver barrierReceiver = new LocationBarrierReceiver();
- registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
3. Define the label for the barrier and add the barrier.
- String locationBarrierLabel = "location enter barrier";
- BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
- BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, enterBarrier,pendingIntent).build();
- Awareness.getBarrierClient(context).updateBarriers(request)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
- Log.e(TAG, "add barrier failed", e);
- }
- });
4. Define the broadcast receiver to listen for the barrier event for further processing.
- class LocationBarrierReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- BarrierStatus barrierStatus = BarrierStatus.extract(intent);
- String label = barrierStatus.getBarrierLabel();
- switch(barrierStatus.getPresentStatus()) {
- case BarrierStatus.TRUE:
- Log.i(TAG, label + " status:true");
- break;
- case BarrierStatus.FALSE:
- Log.i(TAG, label + " status:false");
- break;
- case BarrierStatus.UNKNOWN:
- Log.i(TAG, label + " status:unknown");
- break;
- }
- }
- }