Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

一個 App 兩個圖示:模擬 QQ 郵箱的桌面捷徑方案

編輯:關於 Android 程式設計

需求聽起來有點無厘頭:桌面上要有兩個圖示,看起來像兩個獨立 App,點不同圖示進入不同功能——就像 QQ 郵箱同時提供「郵箱」與「日曆」兩個入口那樣。本文記錄當時的實作思路。

先理解 Launcher 的點擊邏輯

桌面之所以能區分「資料夾」與「應用」,靠的是 View 的 tag。Launcher 的 onClick 大致如下:

public void onClick(View v) {
    Object tag = v.getTag();
    if (tag instanceof ApplicationInfo) {
        // 啟動捷徑
        final Intent intent = ((ApplicationInfo) tag).intent;
        startActivitySafely(intent);
    } else if (tag instanceof FolderInfo) {
        handleFolderClick((FolderInfo) tag);
    }
}

而 startActivitySafely 除了處理 ActivityNotFoundException、SecurityException 這類例外,關鍵是加上了一個 flag:

void startActivitySafely(Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
    } catch (SecurityException e) {
        Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
    }
}

FLAG_ACTIVITY_NEW_TASK 的意義

加上這個 flag 後,系統會先尋找是否存在與待啟動 Activity 具有相同 taskAffinity 的任務堆疊。若存在,就把整個堆疊移到前台並保持內部 Activity 的順序不變;若不存在,才新建一個堆疊來承載它。

這正好解釋了一個常見現象:按 Home 回到桌面後再點圖示,回到的是原本的狀態,而不是重新建立一個全新 Activity。

反過來說,只要讓不同入口分別落在不同的任務堆疊裡,它們彼此就不會互相干擾。這就是整個方案的支點。

實作:用捷徑 + taskAffinity 分流

整體流程是:應用啟動時先建立兩個桌面捷徑,兩者都指向同一個 BootupActivity,但各自帶一個「要開哪個 Activity」的類名參數;BootupActivity 讀到參數後再啟動對應頁面。

public class BootupActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String className = getIntent().getStringExtra("Class");
        if (className == null) {
            // 首次由應用圖示進入:建立捷徑,然後開啟預設頁面
            addShortcutToDesktop(getString(R.string.shopping_app_name),
                    R.drawable.shopping_ic_launcher,
                    Activity1.class.getName(), Activity1.class);
            addShortcutToDesktop(getString(R.string.xiaohua_app_name),
                    R.drawable.xiaohua_ic_launcher,
                    Activity2.class.getName(), Activity2.class);
            startAppProcess(Activity1.class.getName());
        } else {
            // 由桌面捷徑進入:直接開啟指定頁面
            startAppProcess(className);
        }
    }

    private void addShortcutToDesktop(String label, int iconRes,
                                      String destClassName, Class bootupClass) {
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra("duplicate", false);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
        if (iconRes != 0) {
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(this, iconRes));
        }
        Intent intent = new Intent(this, bootupClass);
        intent.putExtra("Class", destClassName);
        intent.setAction(Intent.ACTION_MAIN);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        sendBroadcast(shortcut);
    }

    private void startAppProcess(String bootupClass) {
        Intent i = new Intent();
        i.setComponent(new ComponentName(this.getPackageName(), bootupClass));
        i.putExtra("class", bootupClass);
        this.startActivity(i);
    }
}

建立捷徑靠的是發送 com.android.launcher.action.INSTALL_SHORTCUT 廣播,記得在 manifest 申請權限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

千萬別漏掉的 taskAffinity

manifest 中有幾點必須注意:

1. 每個需要獨立堆疊的 Activity 要設定不同的 android:taskAffinity;
2. 主要 Activity 與預設啟動的 Activity 必須共用同一個 affinity,這樣桌面圖示與應用圖示才會開在同一個堆疊;
3. BootupActivity 要放在清單的第一個位置,其他入口 Activity 則各自加上 action,且與主要 Activity 保持一致。

這個 demo 只是骨架模型,實際專案裡通常還會在 BootupActivity 中插入業務邏輯、初始化工作與登入判斷。但核心不變:靠捷徑 Intent 攜帶參數做分流,靠 taskAffinity 做堆疊隔離。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved