溫馨提示×

溫馨提示×

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

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

Android應(yīng)用中實現(xiàn)多個圖標

發(fā)布時間:2020-12-04 16:59:46 來源:億速云 閱讀:255 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)Android應(yīng)用中實現(xiàn)多個圖標,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1. 多Activity + intent-filter方式

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

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

<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為什么名字, 你通過點擊這個圖標進入的話, 在代碼中

getIntent().getComponent().getClassName()

可以獲取到該名字, targetActivity 就是你點擊該圖標后的目標activity. 上面代碼是寫在目標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)頁標簽-添加快捷方式

這只是針對特殊情形, 比如UC瀏覽器創(chuàng)建一個網(wǎng)頁標簽在桌面上,是向桌面應(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" />

添加圖標

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);

  // 圖標
  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);
 }

看完上述內(nèi)容,你們對Android應(yīng)用中實現(xiàn)多個圖標有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(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