一 gradle 依赖添加
implementation "androidx.car:car:1.0.0-alpha7"
二 在清单文件 所有的activity添加
<meta-data android:name="distractionOptimized" android:value="true" />
表示这个activity 驾驶模式由我们自己来处理
三 在 需要处理的activity 里添加驾驶模式检测 进而控制遮罩非驾驶安全的功能 比如长文本 视频 二维码
public class SoundActivity extends AppCompatActivity {
// 驾驶模式检测
private CarUxRestrictionsHelper carUxRestrictionsHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound);
OnUxRestrictionsChangedListener mUxRestrictionsChangedListener = new OnUxRestrictionsChangedListener() {
@Override
public void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo) {
// 对当前驾驶状态有效的Car UX限制的组合。
int activeRestrictions = restrictionInfo.getActiveRestrictions();
boolean txtLimit = CarUxRestrictions.UX_RESTRICTIONS_LIMIT_STRING_LENGTH == (activeRestrictions & CarUxRestrictions.UX_RESTRICTIONS_LIMIT_STRING_LENGTH);
// 控制二维码 或者视频
boolean videoLimit = CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO == (activeRestrictions & CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO);
if(txtLimit || videoLimit){
// 显示遮罩
}
}
};
carUxRestrictionsHelper = new CarUxRestrictionsHelper(getContext(), mUxRestrictionsChangedListener);
}
@Override
public void onResume() {
super.onResume();
//start watching uxRestriction change 开始检测
carUxRestrictionsHelper.start();
}
@Override
public void onPause() {
super.onPause();
//stop watching uxRestriction change 停止检测
carUxRestrictionsHelper.stop();
}
}
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