android car Driving mode Demo,驾驶者模式

android car Driving mode Demo

GitHub主页

随着当下车联网、物联网的流行,驾驶体验也被推上前台,比如分心措施等等。

如何让车主开车时避免视频、长文本、图片等的干扰,提高驾驶安全成了众多系统厂商的关注点。

对此Google官方也做了对应的支持,针对不同的情况,提供对应的flag给到开发者去做对应处理。

相关依赖是当前Google并没有提上public的,所以在developer平台是无法找到的,不过mvnrepository仓里已经有相关资源了。

示例

参考文档

引用

    implementation 'com.android.support:car:28.0.0-alpha5'
    implementation 'androidx.car:car-cluster:1.0.0-alpha5'
注意
  • androidx和support库是不兼容的,需要将support库转为Androidx,Android studio有提供该功能。操作参考

  • AndroidManifest.xml配置 一定!一定!一定!要记得加这个meta-data属性,是所有的activity,这个是告诉系统,这个activity的驾驶模式由我自己来处理

            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <!-- 下面的meta-data一定要加上->
                <meta-data
                    android:name="distractionOptimized"
                    android:value="true" />
            </activity>
    

使用简介

1. 使用CarUx辅助类 CarUxRestrictionsHelper
lateinit var carHelper: CarUxRestrictionsHelper
2. 添加驾驶模式变化监听 OnUxRestrictionsChangedListener
注意
        carHelper = CarUxRestrictionsHelper(this@MainActivity,
            OnUxRestrictionsChangedListener {
                val a = it.isDistractionOptimizationRequired
                val b = it.activeRestrictions
                //获取允许的最大内容深度级别数或通过单个任务中的任意一个路径查看遍历
                val c = it.maxContentDepth
                //获取在通过单个任务中的任何一个路径进行遍历期间可以向用户显示的最大允许内容项数量
                val d = it.maxCumulativeContentItems
                //获取UX_RESTRICTIONS_LIMIT_STRING_LENGTH施加时可显示的通用字符串的最大长度 。
                val e = it.maxRestrictedStringLength

                tv_content.text = " 驾驶模式限制是否开启:$a \n UX限制的组合code: $b【${getName(b)}】 \n 最大内容深度级别:$c" +
                    "  \n 可向用户显示的最大允许内容:$d \n 长文本限制时可显示的最大长度:$e"
            })
  • 绑定了OnUxRestrictionsChangedListener之后,CarUxRestrictionsHelper会马上回调一次,本次回调内容为当前的信息

  • isDistractionOptimizationRequired: 如果前景活动需要分散优化,则进行传达,true代表需要做分心处理,false为不需要(不需要可能是驾驶模式没有开启、或者停车等状态)

  • activeRestrictions: 对当前驾驶状态有效的Car UX限制的组合 不同的flag代表不同的限制类型

    
    //No specific restrictions in place, but baseline distraction optimization guidelines need to be adhered to when isDistractionOptimizationRequired() is true.
    int	UX_RESTRICTIONS_BASELINE
    
    //All the above restrictions are in effect.
    int	UX_RESTRICTIONS_FULLY_RESTRICTED
    
    //Limit the number of items displayed on the screen.
    int	UX_RESTRICTIONS_LIMIT_CONTENT
    
    /General purpose strings length cannot exceed the character limit provided by getMaxRestrictedStringLength()
    int	UX_RESTRICTIONS_LIMIT_STRING_LENGTH
    
    //No dialpad for the purpose of initiating a phone call.
    int	UX_RESTRICTIONS_NO_DIALPAD
    
    //No filtering a list.
    int	UX_RESTRICTIONS_NO_FILTERING
    
    //No text entry for the purpose of searching etc.
    int	UX_RESTRICTIONS_NO_KEYBOARD
    
    //No setup that requires form entry or interaction with external devices.
    int	UX_RESTRICTIONS_NO_SETUP
    
    //No Text Message (SMS, email, conversational, etc.)
    int	UX_RESTRICTIONS_NO_TEXT_MESSAGE
    
    //No video - no animated frames > 1fps.
    int	UX_RESTRICTIONS_NO_VIDEO
    
    //No text transcription (live or leave behind) of voice can be shown.
    int	UX_RESTRICTIONS_NO_VOICE_TRANSCRIPTION
    
3. CarUxRestrictionsHelper的start和stop
    override fun onResume() {
        super.onResume()
        carHelper.start()
    }

    override fun onPause() {
        super.onPause()
        carHelper.stop()
    }
  • start 一般在活动界面刷新时使用(如果界面存在刷新的可能,如activity,则建议在onResume())

  • stop 一般在界面停止活动的时候使用 (如activity,则建议在onPause())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值