編輯:關於 Android 程式設計
需求聽起來有點無厘頭:桌面上要有兩個圖示,看起來像兩個獨立 App,點不同圖示進入不同功能——就像 QQ 郵箱同時提供「郵箱」與「日曆」兩個入口那樣。本文記錄當時的實作思路。
桌面之所以能區分「資料夾」與「應用」,靠的是 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 具有相同 taskAffinity 的任務堆疊。若存在,就把整個堆疊移到前台並保持內部 Activity 的順序不變;若不存在,才新建一個堆疊來承載它。
這正好解釋了一個常見現象:按 Home 回到桌面後再點圖示,回到的是原本的狀態,而不是重新建立一個全新 Activity。
反過來說,只要讓不同入口分別落在不同的任務堆疊裡,它們彼此就不會互相干擾。這就是整個方案的支點。
整體流程是:應用啟動時先建立兩個桌面捷徑,兩者都指向同一個 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" />
manifest 中有幾點必須注意:
1. 每個需要獨立堆疊的 Activity 要設定不同的 android:taskAffinity;
2. 主要 Activity 與預設啟動的 Activity 必須共用同一個 affinity,這樣桌面圖示與應用圖示才會開在同一個堆疊;
3. BootupActivity 要放在清單的第一個位置,其他入口 Activity 則各自加上 action,且與主要 Activity 保持一致。
這個 demo 只是骨架模型,實際專案裡通常還會在 BootupActivity 中插入業務邏輯、初始化工作與登入判斷。但核心不變:靠捷徑 Intent 攜帶參數做分流,靠 taskAffinity 做堆疊隔離。
如何在 ES 檔案瀏覽器裡使用網盤
ES 檔案瀏覽器是一款多功能的檔案/程式/行程管理器,能在手機、電腦、遠端與藍牙之間瀏覽管理檔案。除了本機與區網,它還能直接接入各家業者提供的網盤,把雲端空間當成一個普通
Android 自定義控件實戰:手繪柱形圖與折線圖
概述 前幾天突然需要製作兩種圖表——柱形圖與折線圖,第一反應是先找現成的。網上雖然找得到,卻都不是想要的樣子,而且大多不是用純 Android 程式碼完成。HTML5 固
玩轉 Android Camera(五):用 Google 內建人臉檢測即時畫框
本文示範如何呼叫 Camera 內建的 FaceDetectionListener 進行人臉檢測,並把偵測到的人臉用矩形框繪製在預覽畫面上。程式碼基於 PlayCamer
仿微信通訊錄:右側字母表快速定位控件的實現
很多應用的通訊錄界面(如 MIUI 通訊錄)都有右側字母快速定位功能,這其實是一個自定義 View,並不算特別複雜。 先看效果圖: 下面介紹實現思路。自定義 View