最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中一個應(yīng)用實現(xiàn)多個圖標(biāo)的幾種方式

 更新時間:2017年05月04日 14:14:48   作者:Anderson/Jerey_Jobs  
這篇文章主要給大家介紹了在Android中一個應(yīng)用如何實現(xiàn)多個圖標(biāo)的幾種方式,其中包括了多Activity + intent-filter方式、activity-alias方式以及網(wǎng)頁標(biāo)簽-添加快捷方式,分別給出了詳細的示例代碼,需要的朋友可以參考借鑒。

前言

最近因為新需求需要,我的應(yīng)用將有多個ICON入口..最終選擇了 activity-alias , 其實實現(xiàn)多圖標(biāo)有好幾種方式,下面就給大家總結(jié)下,分享出來供大家參考學(xué)習(xí):

1. 多Activity + intent-filter方式

因為launcher會掃描app中含有以下intent-filter屬性的標(biāo)簽, 有的話就會將其添加到桌面.

所以只要在你想添加到桌面的activity下加上以下標(biāo)簽即可.

<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

2. activity-alias方式

上面第一種方式對于一個activity的要求是沒法做的, 只能通過多寫幾個入口Activity+ 跳轉(zhuǎn)參數(shù)的方式來解決, 而 activity-alias方式就可以很好解決該問題.

activity-alias中, android:name 就是你定義這個activity為什么名字, 你通過點擊這個圖標(biāo)進入的話, 在代碼中

getIntent().getComponent().getClassName()

可以獲取到該名字, targetActivity 就是你點擊該圖標(biāo)后的目標(biāo)activity. 上面代碼是寫在目標(biāo)activity里面的,獲取到的名字依然是我們定義的名字哦. 這樣就可以通過這個判斷是通過哪個入口進來的了.

<activity-alias
 android:name="@string/altman"
 android:exported="true"
 android:icon="@drawable/speech_01"
 android:label="@string/altman_app_name"
 android:screenOrientation="landscape"
 android:targetActivity="com.avatar.dialog.DialogActivity"
 android:theme="@style/DialogActivityTheme" >
 <intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity-alias>

3. 網(wǎng)頁標(biāo)簽-添加快捷方式

這只是針對特殊情形, 比如UC瀏覽器創(chuàng)建一個網(wǎng)頁標(biāo)簽在桌面上,是向桌面應(yīng)用(launcher)發(fā)送相關(guān)action的廣播,相關(guān)的action如下:

public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

需要的權(quán)限:

<!-- 添加快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 移除快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<!-- 查詢快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

添加圖標(biāo)

private void addShortcut(String name) {
  Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);

  // 不允許重復(fù)創(chuàng)建
  addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測試不是根據(jù)快捷方式的名字判斷重復(fù)的
  // 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
  // 但是名稱不同時,雖然有的手機系統(tǒng)會顯示Toast提示重復(fù),仍然會建立快鏈
  // 屏幕上沒有空間時會提示
  // 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式

  // 名字
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

  // 圖標(biāo)
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
    Intent.ShortcutIconResource.fromContext(MainActivity.this,
      R.drawable.ic_launcher));

  // 設(shè)置關(guān)聯(lián)程序
  Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
  launcherIntent.setClass(MainActivity.this, MainActivity.class);
  launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  addShortcutIntent
    .putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);

  // 發(fā)送廣播
  sendBroadcast(addShortcutIntent);
 }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論

右玉县| 遵义市| 达州市| 含山县| 隆林| 清水县| 玉环县| 连江县| 墨竹工卡县| 巴林右旗| 南召县| 吴忠市| 屯门区| 祁连县| 安化县| 鲁山县| 吉水县| 儋州市| 万山特区| 阿瓦提县| 石泉县| 泸定县| 巨野县| 黑龙江省| 嵊泗县| 交口县| 和林格尔县| 永兴县| 建宁县| 中超| 鹤山市| 辛集市| 湘潭县| 图片| 长垣县| 武邑县| 惠东县| 句容市| 江口县| 忻州市| 南安市|