让你的音乐APP脱颖而出,更懂用户,也更动人心、java
做为音乐发烧友,闲下来的时候总想打开App,享受沉浸在音乐中的放松。然而,App推荐的歌单常常无法知足个人须要,若是App能根据我当前的情景状态,推送给个人歌曲刚好就是我当前最想听的,那该多好啊~segmentfault
情景感知服务(Awareness Kit)能感知用户当前的时间、地理位置、活动状态、耳机状态、天气情况、环境光、车载链接状态、信标链接状态等情景,并经过能常驻后台运行的围栏能力向APP进行提醒,使APP能第一时间给用户提供精准和贴心的服务。上述情景感知能力还在不断扩充中,并且您能够自由组合这些感知能力,构建组合围栏,从而让APP的服务能力更加智能,更加精准。app
在情景感知服务的支持下,App能给用户带来以下体验ide
同时,用户还能够经过各类感知能力的组合围栏,设置排除场景,避免给用户过多打扰。ui
无需用户提早开启App,用户进入地理围栏范围后,便可后台激活App,触发通知。this
无惧App进程被系统杀死,经过围栏服务,依然可接受到通知。code
点击通知,便可前台激活APP,点击直接跳转App推荐界面。接口
经过组合围栏实现精准推送;也可避免在用户不须要的场景提供无效通知,避免频繁打扰。进程
Awareness Kit集成须要有以下3个关键步骤,能够参考华为开发者联盟的文档事件
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/awareness-preparation
//建立一个感知耳机链接的围栏,当耳机处于链接状态时,此围栏的状态为true AwarenessBarrier headsetBarrier = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED); //建立一个PendingIntent,当围栏状态改变的时候,会触发该PendingIntent,这里以发送广播为例 PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); //建立围栏的标签,后续能够经过标签查询或者删除对应围栏 String headsetBarrierLabel = "headset keeping connected label";
//把刚刚建立好的耳机围栏和它对应的label,pendingIntent注册给Awareness kit Awareness.getBarrierClient(context).updateBarriers(new BarrierUpdateRequest.Builder() .addBarrier(headsetBarrierLabel,headsetBarrier,pendingIntent).build()) .addOnSuccessListener(aVoid -> { //注册围栏成功 Log.i(TAG,"add barrier success"); }) .addOnFailureListener(e -> { //注册围栏失败 Log.e(TAG,"add barrier failed"); e.printStackTrace(); });
//本示例中咱们耳机围栏的PendingIntent设置的是发送广播,因此须要定义对应的广播接收器来监听围栏的状态 public final class HeadsetBarrierReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //围栏的信息经过intent传递过来,咱们经过Barrier.extract方法将其解析出来 BarrierStatus barrierState = BarrierStatus.extract(intent); //经过BarrierStatus获取label和围栏的当前状态 String label = barrierState.getBarrierLabel(); int status = barrierState.getPresentStatus(); if (status == BarrierStatus.TRUE && label.equals(headsetBarrierLabel)) { //当围栏状态为true时,表明耳机处于链接状态,这时就能够在通知栏推送相关消息 //send Notification.... } } }
定义完广播接收器后别忘记注册该广播接收器,若是须要APP被杀后依然推送,能够把该接收器设置为静态广播接收器。
在用户点击通知打开app后,能够经过Awareness各个能力的快照接口来获取用户当前的情景状态以推荐不一样歌单。
例如获取时间情景状态:
Awareness.getCaptureClient(context).getTimeCategories() .addOnSuccessListener(timeIntervalsResponse -> { TimeCategories categories = timeIntervalsResponse.getTimeCategories(); if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_HOLIDAY)) { //当天是节假日,可推荐节假日歌单 } if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_WEEKEND)) { //当天是周末,可推荐周末歌单 } if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_NIGHT)) { //当前是深夜,可推荐深夜歌单 } }) .addOnFailureListener(e -> { //获取时间信息失败 Log.e(TAG, "get Time Categories failed"); e.printStackTrace(); });
获取用户当前的活动状态以推荐歌单:
Awareness.getCaptureClient(context).getBehavior() .addOnSuccessListener(behaviorResponse -> { BehaviorStatus behaviorStatus = behaviorResponse.getBehaviorStatus(); DetectedBehavior mostLikelyBehavior = behaviorStatus.getMostLikelyBehavior(); String str = "Most likely behavior is " + mostLikelyBehavior.getType(); }) .addOnFailureListener(e -> { //获取活动状态失败 Log.e(TAG, "Failed to get the behavior.", e); });
获取当前是不是链接车载蓝牙:
int deviceType = 0; // 0 表明获取的设备类型为车载蓝牙 Awareness.getCaptureClient(this).getBluetoothStatus(deviceType) .addOnSuccessListener(bluetoothStatusResponse -> { BluetoothStatus bluetoothStatus = bluetoothStatusResponse.getBluetoothStatus(); int status = bluetoothStatus.getStatus(); if (status == BluetoothStatus.CONNECTED) { //当前是链接车载蓝牙,可将app切换为车载模式 } }) .addOnFailureListener(e -> { //获取车载蓝牙状态失败 Log.e(TAG, "Failed to get Bluetooth status.", e); });
往期连接:一文搞懂文本识别、银行卡识别、通用卡证识别、身份证识别
内容来源:https://developer.huawei.com/consumer/cn/forum/topicview?fid=18&tid=0201246052748810283&pid=0301246052748810253原做者:Ascend