汽車設定中的大多數頁面都是作為一系列擴展SettingsFragment
片段實現的,每個片段都有自己在CarSettingActivities
中定義的活動。這些靜態活動是從BaseCarSettingsActivity
擴展的。除了這些設定之外,您還可以注入其他系統應用程式的首選項以顯示在 CarSettings 中。
在汽車設定中新增的首選項
若要新增設定:
- 定義一個 XML 檔:
- 確保所有首選項均已定義
android:key
。鍵列表保存在preference_keys.xml
中。首選項鍵應該是唯一的。 - 出於搜尋索引的目的,首選項畫面還應該有一個定義的
android:key
。首選項畫面鍵清單保存在preference_screen_keys.xml
中。首選項螢幕鍵也應該是唯一的。 - 如果首選項僅顯示靜態資訊(例如,沒有特殊的業務邏輯),請將首選項控制器設定為
com.android.car.settings.common.DefaultRestrictionsPreferenceController
。 - 如果首選項需要業務邏輯,請使用新的首選項控制器名稱設定首選項控制器。
- 確保所有首選項均已定義
- (如果需要)在適當的套件中建立首選項控制器,該套件擴展了
PreferenceController
。如果需要,請參閱 Javadoc。 - 使用
getPreferenceScreenResId
建立一個片段,傳回步驟 1 中定義的 XML 檔案。 - 在
CarSettingActivities
中建立一個擴充BaseCarSettingsActivity
活動,然後實作getInitialFragment()
,傳回步驟 3 中定義的片段。 - 更新
AndroidManifest.xml
以包含步驟 4 定義的活動。
例子
以下材料說明了此過程。
- 定義名為
demo_fragment.xml
的 XML 檔案:<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:settings="http://schemas.android.com/apk/res-auto" android:title="@string/demo_label" android:key="@string/psk_demo"> <Preference android:icon="@drawable/ic_settings_demo_preference_1" android:key="@string/pk_demo_preference_1" android:title="@string/demo_preference_1_title" settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"> <intent android:targetPackage="com.android.car.settings" android:targetClass="com.android.car.settings.common.CarSettingActivities$DemoSetting1Activity"/> </Preference> <Preference android:icon="@drawable/ic_settings_demo_preference_2" android:key="@string/pk_demo_preference_2" android:title="@string/demo_preference_2_title" settings:controller="com.android.car.settings.example.MyCustomRestrictionsPreferenceController"> <intent android:targetPackage="com.android.car.settings" android:targetClass="com.android.car.settings.common.CarSettingActivities$DemoSetting2Activity"/> </Preference> </PreferenceScreen>
- 將首選項鍵新增至
preference_keys
:<resources> [...] <string name="pk_demo_preference_1" translatable="false">demo_preference_1</string> <string name="pk_demo_preference_2" translatable="false">demo_preference_2</string> </resources>
- 將首選項畫面鍵新增至
preference_screen_keys.xml
:<resources> [...] <string name="psk_demo" translatable="false">demo_screen</string> </resources>
對於第一個範例首選項,請使用
DefaultRestrictionsPreferenceController
。對於第二個首選項,使用需要定義的自訂首選項控制器。對於本範例,您可以僅為可用的管理員使用者自訂此首選項。為此,請定義以下自訂控制器:public class MyCustomRestrictionsPreferenceController extends PreferenceController<Preference> { private final UserManager mUserManager; public MyCustomRestrictionsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions) { super(context, preferenceKey, fragmentController, uxRestrictions); mUserManager = UserManager.get(context); } @Override protected Class<Preference> getPreferenceType() { return Preference.class; } @Override public int getAvailabilityStatus() { return mUserManager.isAdminUser() ? AVAILABLE : DISABLED_FOR_USER; } }
- 若要建立片段,請覆寫
getPreferenceScreenResId
: - 若要儲存新片段,請在
CarSettingActivities
中建立一個活動: - 使用新活動更新清單檔案:
- 未直接在 CarSettings 應用程式中實作(例如注入由 OEM 實現的設定)。
- 應出現在 CarSettings 應用程式中。
- 若要將活動標記為注入設置,請為活動新增意圖過濾器。
- 告訴 CarSettings 應用程式它屬於哪個類別。類別是常數,在
CategoryKey
中定義,用於指示注入的設定應出現在 CarSettings 的哪個層級。我們在CategoryKey
內提供了一組類別,但 OEM 可以定義自己的類別,但沒有任何限制。 - (可選)顯示設定時新增摘要文字:
<activity android:name="Settings$DemoSettingsActivity" <!-- Mark the activity as an injected setting --> <intent-filter> <action android:name="com.android.settings.action.EXTRA_SETTINGS"/> </intent-filter> <!-- Tell CarSettings app which category it belongs to --> <meta-data android:name="com.android.settings.category" android:value="com.android.settings.category.demo_category"/> <!-- Tell CarSettings the what the preference title should be --> <meta-data android:name="com.android.settings.title" android:value="@string/app_name" /> <!-- Optional: specify the icon to show with the preference --> <meta-data android:name="com.android.settings.icon" android:resource="@drawable/ic_demo" android:value="true"/> <!-- Optional: Add a summary text when the string is displayed --> <meta-data android:name="com.android.settings.summary" android:resource="@string/demo_summary"/> </activity>
public class DemoFragment extends SettingsFragment { @Override @XmlRes protected int getPreferenceScreenResId() { return R.xml.demo_fragment; } }
public class CarSettingActivities { [...] public static class DemoActivity extends BaseCarSettingsActivity { @Nullable @Override protected Fragment getInitialFragment() { return new DemoFragment(); } } }
<application [...] <activity android:name=".common.CarSettingActivities$DemoActivity" android:exported="true"> <meta-data android:name="distractionOptimized" android:value="true"/> </activity> [...] </application>
在汽車設定中新增外部意圖首選項
作為注入首選項的替代方案,也可以將首選項直接插入到旨在插入另一個應用程式的「汽車設定」中。這可以透過簡單地將首選項新增至首選項畫面以及解析為外部應用程式的意圖操作來完成。與「汽車設定」中的其他首選項一樣,這些首選項具有相同的可用 XML 屬性。
<Preference android:key="@string/pk_demo_preference" android:title="@string/demo_preference_title" android:summary="@string/demo_preference_summary" settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"> <intent android:action="android.intent.action.DEMO_ACTION"/> </Preference>
新增註入的首選項
注入的偏好包含導致外部或內部活動的意圖。例如,設定主頁上的Google設定項目就是注入的首選項。當滿足以下任一條件時,注入的首選項特別有用。那個設定:
若要將活動配置為注入設定:
若要讓注入的設定顯示在 CarSettings 應用程式的特定頁面上,請在 XML 中包含下列範例程式碼,並在適當的情況下修改變數:
<com.android.car.settings.common.LogicalPreferenceGroup <!-- Replace key string --> android:key="@string/pk_system_extra_settings" <!-- Indicates the preferences in the group should be injected in. ExtraSettingsPreferenceController contains the logic to pull in injected preferences. --> settings:controller="com.android.settings.common.ExtraSettingsPreferenceController"> <!-- Tells the controller what activities should be pulled into this preference group. --> <intent android:action="com.android.settings.action.EXTRA_SETTINGS"> <!-- Name and value should match the metadata in your activity --> <extra android:name="com.android.settings.category" android:value="com.android.settings.category.demo_category"/> </intent> </com.android.car.settings.common.LogicalPreferenceGroup>