Foreword

Some time ago, I watched a documentary "Chinese Tourists in Paris", which described the "Chinese-style tourism" in the eyes of foreigners: I am keen to check in at scenic spots, indulge in taking pictures, and I have no time to appreciate it carefully; if you leave enough time, buy and buy, you can't waste your trip. Netizens summed up the Chinese-style travel "get in the car to sleep, get out of the car to take pictures, and don't know anything when you get home." I hurriedly "reflected" and felt that it was purely because I was lazy and didn't want to be a travel guide, so I could only follow the group to the "people follow the crowd" scenic spot to admire the back of others' heads. At this moment, I want to have an all-round "butler" who can recommend travel itineraries, timely travel tips, recommend food, clothing, and housing information.

Here comes the point. The Awareness Kit situational awareness service launched by Huawei can fully implement these functions and services and is completely free. This is simply an indispensable artifact for the benefit of lazy travel!

Tourism application scenarios
The HUAWEI Awareness Kit provides the ability to obtain the user’s current time, location, activity status, audio device status, ambient light, weather, beacon and other contextual awareness capabilities, and invokes these capabilities to help the App faster and more efficiently Gain insight into the user’s current situation and provide a smarter and caring experience.

When traveling, with the support of Awareness Kit, weather awareness reminds users to prepare suitable travel clothes, location awareness recommends travel, dining, attractions, ticketing and other information, time and weather awareness reminds users of sunrise/sunset and other information Super considerate travel companion service.

Key development code

Development preparation

1. Configure AppGallery Connect.

2. Integrate HMS Core SDK.

3. Configure the obfuscated script.

Refer to the official website for specific operations: Configure AppGallery Connect

Interface call code

1, designated permissions

When calling capabilities such as location, weather, and time, the developer needs to specify the corresponding permissions in the Manifest first.

2. Import interface class

When developers use various perception capabilities, they need to import the public capabilities of context-aware services and classes related to the status of various functions. For example: time-awareness import interface class

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hms.kit.awareness.Awareness;
//导入时间快照相关类
import com.huawei.hms.kit.awareness.capture.TimeCategoriesResponse;
import com.huawei.hms.kit.awareness.status.TimeCategories;
//导入时间围栏相关类
import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
import com.huawei.hms.kit.awareness.barrier.BarrierStatus;
import com.huawei.hms.kit.awareness.barrier.TimeBarrier;
import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

3. Capture capability development

The Capture API allows apps to request the user's current status, such as the user's current time, location, activity status, device status, etc.

(1) Obtain the "Capture Client" of the Awareness kit.

(2) Query the situation status through the "Capture Client" calling the time status query capability interface.

(3) Monitor the return of the context-aware service result, and perform the business processing of the application.

For example: weather awareness

Awareness.getCaptureClient(this).getWeatherByDevice()
        .addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
            @Override
            public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
                WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
                WeatherSituation weatherSituation = weatherStatus.getWeatherSituation();
                Situation situation = weatherSituation.getSituation();
                // 更多返回的天气数据信息可参考华为开发者文档
                String weatherInfoStr = "City:" + weatherSituation.getCity().getName() + "\n" +
                        "Weather id is " + situation.getWeatherId() + "\n" +
                        "CN Weather id is " + situation.getCnWeatherId() + "\n" +
                        "Temperature is " + situation.getTemperatureC() + "℃" +
                        "," + situation.getTemperatureF() + "℉" + "\n" +
                        "Wind speed is " + situation.getWindSpeed() + "km/h" + "\n" +
                        "Wind direction is " + situation.getWindDir() + "\n" +
                        "Humidity is " + situation.getHumidity() + "%";
                Log.i(TAG, weatherInfoStr);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.e(TAG, "get weather failed");
            }
        });

4. Barrier capability development

The Barrier API can set the "fence" of the contextual state for the app. When the user's state reaches the preset contextual state, the context-aware service will trigger the app to send a notification.

Take the development of the "Barrier" of the "enter" of the geofence as an example, the barrier is triggered after entering the set location range.

(1) Define Barrier.

AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);

(2) Define the "PendingIntent" triggered when the Barrier state changes.

(3) Define the label Label corresponding to the Barrier, and then 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, which is used to monitor the Barrier event, and perform the business processing of the application after receiving the event.

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;
        }
    }
}

The above is the sample code for calling some functions of the interface.

For more detailed development guidelines, please refer to Huawei Developer Alliance- Context Awareness Service Official Website

Concluding remarks

Huawei's contextual awareness service is widely used in apps such as travel, sports, health, music, games, and photography by calling functions such as time, location, weather, activity status, and device status to provide users with a smarter and more intimate experience.

[Scenario Awareness Service] Introduction of boutique development cases

awareness service] 1616d3ea6a4e7c Let your music APP stand out,

[Scenario Awareness Service] gives "buy, buy, buy" a new way to open

Visit Huawei Developer Alliance official website
Obtain development guide document
Huawei Mobile Services open source warehouse address: GitHub , Gitee

and learn about the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。


引用和评论

0 条评论