Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

用 PackageManager 取得所有啟動器應用清單

編輯:初階開發

要列出裝置上所有出現在桌面(Launcher)裡的應用,關鍵是 PackageManager 搭配一個特定的 Intent 組合。以下把實作拆成幾個步驟。

1. 定義一個承載應用資訊的內部類別

用 LauncherItem 描述每個應用的圖示、名稱與 ComponentName:

public class LauncherItem {

    Drawable icon;
    String name;
    ComponentName component;

    LauncherItem(Drawable d, String s, ComponentName cn) {
        icon = d;
        name = s;
        component = cn;
    }
}

ComponentName 是這裡最關鍵的欄位——它是「套件名 + 類別名」的組合,有了它才能精準啟動某個應用,不必依賴隱式 Intent 去猜。

2. 查詢並收集結果

List<LauncherItem> lvalue;

public void addLauncher() {

    lvalue = new ArrayList<LauncherItem>();
    pkgMgt = this.getPackageManager();

    // 用 ACTION_MAIN + CATEGORY_LAUNCHER 查詢所有啟動器項目
    Intent it = new Intent(Intent.ACTION_MAIN);
    it.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);

    for (int i = 0; i < ra.size(); i++) {
        ActivityInfo ai = ra.get(i).activityInfo;

        Drawable icon = ai.loadIcon(pkgMgt);
        String label = ai.loadLabel(pkgMgt).toString();
        ComponentName c = new ComponentName(
                ai.applicationInfo.packageName, ai.name);

        LauncherItem item = new LauncherItem(icon, label, c);
        lvalue.add(item);
    }
}

這裡的原理值得說明。Android 判斷「哪些 Activity 該出現在桌面」的依據,就是它在 Manifest 中是否同時宣告了 ACTION_MAIN 這個 action 與 CATEGORY_LAUNCHER 這個 category。所以我們只要送出一個帶有這兩項條件的 Intent,系統就會回傳所有符合的候選。

loadIcon() 與 loadLabel() 分別取得圖示與顯示名稱。要注意這兩個呼叫會進行資源載入,應用數量多時成本不低,建議只在需要時載入一次並快取起來,不要在 getView 中反覆呼叫。

3. 定義 Adapter 決定顯示樣式

public class LauncherAdapter extends BaseAdapter {

    Activity activity;

    public LauncherAdapter(Activity a) {
        activity = a;
    }

    @Override
    public int getCount() {
        return lvalue.size();
    }

    @Override
    public Object getItem(int position) {
        return lvalue.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return composeItem(position);
    }

    public View composeItem(int position) {
        LinearLayout layout = new LinearLayout(activity);
        layout.setOrientation(LinearLayout.HORIZONTAL);

        ImageView iv = new ImageView(activity);
        iv.setImageDrawable(lvalue.get(position).icon);
        layout.addView(iv);

        TextView tv = new TextView(activity);
        tv.setText(lvalue.get(position).name);
        tv.setPadding(10, 5, 0, 0);
        layout.addView(tv);

        return layout;
    }
}

這裡每列是「圖示 + 名稱」的水平排列。不過 composeItem 每次都重新建立整組檢視,沒有利用 convertView,長清單會有明顯的效能問題——後面會給出改善寫法。

4. 掛上 Adapter 並處理點擊

adapter = new LauncherAdapter(this);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setComponent(lvalue.get(arg2).component);
        startActivity(intent);
    }
});

點擊時用 setComponent() 指定明確的目標元件,再送出去啟動。這裡用 ACTION_VIEW 即可;若想語意更精確,也可以用 ACTION_MAIN 搭配 addCategory(CATEGORY_LAUNCHER),或直接改用 getLaunchIntentForPackage()(見下方說明)。

5. 模擬器執行結果

列出所有應用:

單擊 Alarm Clock 的情形:

改善與注意事項

getView 應該重用 convertView。改寫成標準的 ViewHolder 模式:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(activity)
                .inflate(R.layout.launcher_item, parent, false);
        holder = new ViewHolder();
        holder.icon = convertView.findViewById(R.id.icon);
        holder.label = convertView.findViewById(R.id.label);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.icon.setImageDrawable(lvalue.get(position).icon);
    holder.label.setText(lvalue.get(position).name);
    return convertView;
}

版面也建議改用 XML 定義而非純程式碼組裝,樣式調整會方便很多。

啟動方式有現成 API。若只是要「啟動某個套件的預設入口」,不必自己組 ComponentName:

PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.android.deskclock");
if (launchIntent != null) {
    startActivity(launchIntent);
}

這個方法會回傳 null(代表該套件沒有可啟動的 Activity),使用前務必判斷,否則會 NullPointerException。

Android 11 的套件可見性限制。從 API 30 開始,queryIntentActivities() 預設只會看到少數與你有互動過的應用。若要列出全部啟動器項目,必須在 Manifest 中明確宣告查詢意圖:

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

若你做的是「桌面」「啟動器」這類真的需要看到所有應用的 App,才考慮申請 QUERY_ALL_PACKAGES 權限——Google Play 對這個權限有明確政策限制,用途不符會被拒。

圖示載入請放背景。大量 Drawable 在主執行緒載入會造成畫面卡頓,建議用背景執行緒預先載入並快取,或直接採用 Glide、Coil 這類圖片載入庫搭配 app icon 的 URI。

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