溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解Android 7.0 Settings 加載選項

發(fā)布時間:2020-09-28 14:06:46 來源:腳本之家 閱讀:316 作者:nearbyYoung 欄目:移動開發(fā)

先寫在前面,這說的Settings加載選項是指Settings這個應(yīng)用顯示在主界面的選項,這個修改需要對系統(tǒng)源碼進行修改。

Android 7.0 Settings頂部多了一個建議選項,多了個側(cè)邊欄,操作更加便捷了。

詳解Android 7.0 Settings 加載選項      詳解Android 7.0 Settings 加載選項

                  原生7.0主界面                                                          原生7.0側(cè)邊欄

Android 6.0

之前做Android 6.0開發(fā)的,都會了解到6.0的Settings加載選項是通過加載dashboard_categories.xml,獲取需要顯示的選項,并且在SettingsActivity中也進行判斷是否要顯示,所以在6.0上添加一個選項是比較簡單的,直接在dashboard_categories.xml添加icon、title、summary,也可以添加目標(biāo)fragment和Intent,這樣就可以順利跳轉(zhuǎn)到對應(yīng)的界面了。但是在7.0上,google對Settings進行了重構(gòu)。

Android 7.0

7.0的Settings的選項不再從dashboard_categories.xml中加載選項列表,而是通過在Androidmanifest.xml中,配置intent-filter的Action,在通過PackageManager進行指定的Action進行搜索,那么就可以獲取到需要顯示的選項列表了,并且也需要在代碼中進行判斷,判斷哪些功能需要顯示與否。

TileUtils.Java中通過幾個Action進行獲取系統(tǒng)中對應(yīng)的activity,如Settings中的幾個

private static final String SETTINGS_ACTION ="com.android.settings.action.SETTINGS";

private static final String OPERATOR_SETTINGS ="com.android.settings.OPERATOR_APPLICATION_SETTING";

private static final String OPERATOR_DEFAULT_CATEGORY ="com.android.settings.category.wireless";

private static final String MANUFACTURER_SETTINGS ="com.android.settings.MANUFACTURER_APPLICATION_SETTING";

private static final String MANUFACTURER_DEFAULT_CATEGORY ="com.android.settings.category.device";

通過PackageManager進行搜索,獲取到這一系列的activity信息,

PackageManager pm = context.getPackageManager();
List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,PackageManager.GET_META_DATA, user.getIdentifier());

并且在AndroidManifest.xml通過meta-data配置了icon、title、summary,那這就有Settings中顯示的圖標(biāo)、標(biāo)題和說明。還有在Settings中顯示的分類、目標(biāo)Fragment。

Settings 的AndroidManifest.xml 下面那代碼是Settings->about phone的Activity配置。

<activity android:name="Settings$DeviceInfoSettingsActivity"
        android:theme="@style/Theme.SubSettingsDialogWhenLarge"
        android:label="@string/device_info_settings"
        android:icon="@drawable/ic_settings_about"
        android:taskAffinity="com.android.settings"
        android:parentActivityName="Settings">

      <intent-filter android:priority="1">
        <action android:name="android.settings.DEVICE_INFO_SETTINGS" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.VOICE_LAUNCH" />
      </intent-filter>
      <intent-filter android:priority="-1">
        <action android:name="com.android.settings.action.SETTINGS" />
      </intent-filter>

      <meta-data android:name="com.android.settings.category"
        android:value="com.android.settings.category.system" />
      <meta-data android:name="com.android.settings.title"
        android:resource="@string/about_settings" />
      <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
        android:value="com.android.settings.DeviceInfoSettings" />

      <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
        android:value="true" />
    </activity>

Settings顯示各選項的思路比較簡單,所以直接在Settings里面添加功能選項還是比較簡單的。

添加第三方apk到Settings選項中

如果需要Settings中添加打包好的apk,需要三個步驟。

1. 添加action。

可以被PackageManager搜索到的activity,搜索到后添加到Settings的選項列表中

復(fù)制代碼 代碼如下:

private static final String EXTRA_SETTINGS_ACTION ="com.android.settings.action.EXTRA_SETTINGS";

1、添加顯示的選項信息。

在該apk的AndroidManifest.xml對應(yīng)的啟動activity中添加以下meta-data。最好是有該apk提供公司的技術(shù)支持,不然有混淆的代碼就比較難進行更改、回編譯了。

  /**
   * The key used to get the category from metadata of activities of action
   * {@link #EXTRA_SETTINGS_ACTION}
   * The value must be one of:
   * <li>com.android.settings.category.wireless</li>
   * <li>com.android.settings.category.device</li>
   * <li>com.android.settings.category.personal</li>
   * <li>com.android.settings.category.system</li>
   */
  private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";

  /**
   * Name of the meta-data item that should be set in the AndroidManifest.xml
   * to specify the icon that should be displayed for the preference.
   */
  public static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";

  /**
   * Name of the meta-data item that should be set in the AndroidManifest.xml
   * to specify the title that should be displayed for the preference.
   */
  public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";

  /**
   * Name of the meta-data item that should be set in the AndroidManifest.xml
   * to specify the summary text that should be displayed for the preference.
   */
  public static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";

在TileUtils.java的白名單中添加對應(yīng)的包名。

這個也是Android考慮到的一些安全上的問題,沒有添加到白名單上面的包名,就不會顯示出來。

  /// Extra package white list for add item to Settings @{
  private static final String[] EXTRA_PACKAGE_WHITE_LIST = {};
  /// @}

最后就是全編,刷機驗證了。

相比android6.0,在7.0上添加功能選項顯得更簡單了,只需要寫好了功能,在Settings的AndroidManifest.xml中添加必要的參數(shù),或者是在獨立的apk中AndroidManifest.xml中配置必要的參數(shù),再在TileUtils.java中添加包名就好了。需要修改的地方更少了,并且Google將很方方法放到了com.android.settinglibs里,精簡了一部分代碼。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI