アプリケーションリストを取得する

インストールしているアプリが多すぎて表示が遅くて困る。

取得コード

参考になりそうなものを集める。

// 起動可能なIntent
Intent intent=new Intent(Intent.ACTION_MAIN,null);
// デスクトップから可能なIntent(つまり通常のアプリケーション)
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通常のアプリケーションのリストを取得
PackageManager manager=getPackageManager();
List<ResolveInfo6gt; infoes=manager.queryIntentActivities(intent,0);
TextView textView = (TextView) findViewById(R.id.textView);
for (int i = 0;i < infes.size(); i++) {
  ResolveInfo info=infes.get(i);
  // TextViewにアプリケーション名を出力
  textView.append(info.loadLabel(manager) + "\n");
}

あらかじめインテントにアプリの属性をセット。

    //アプリリストの読み込み
    private void loadAppList() {
        //アアクティビティ情報のリストの取得
        PackageManager manager=getPackageManager();
        Intent intent=new Intent(Intent.ACTION_MAIN,null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);        
        List<ResolveInfo> apps=manager.queryIntentActivities(intent,0);
        Collections.sort(apps,new ResolveInfo.DisplayNameComparator(manager));

        //アクティビティ情報の取得
        appList=new ArrayList<AppInfo>();
        if (apps==null) return;
        for (int i=0;i<apps.size();i++) {
            AppInfo appInfo=new AppInfo();
            ResolveInfo info=apps.get(i);
            appInfo.title=info.loadLabel(manager);
            appInfo.setActivity(
                info.activityInfo.applicationInfo.packageName,
                info.activityInfo.name);
            appInfo.icon=resizeIcon(info.activityInfo.loadIcon(manager));
            appList.add(appInfo);
        }
        
        //グリッドの更新
        gridView.setAdapter(new GridAdapter(this));
    } 

ブロードキャストレシーバで受けるほうがいいのか?

PackageManager pm = getPackageManager();
List<resolveinfo> resolveInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_SEARCH), PackageManager.MATCH_DEFAULT_ONLY);
StringBuffer sb = new StringBuffer();
for(int i = 0; i < resolveInfo.size(); i++) {
  ResolveInfo info = resolveInfo.get(i);
  ActivityInfo activityinfo = info.activityInfo;
  sb.append(activityinfo.packageName + "\n");
}        
textView.setText(sb.toString());  


PackageManager pm = getPackageManager();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<resolveinfo> resolveInfo = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

// sort
Collections.sort(resolveInfo, new ResolveInfo.DisplayNameComparator(pm));

ACTION_SEND などは、setType() しないと取得できないらしい。
http://y-anz-m.blogspot.com/2010/03/android-action.html

// アクティビティリスト取得
PackageManager pm = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

//指定されたインテントフラグでアクティビティを検索
List<ResolveInfo> appList= pm.queryIntentActivities(intent, 0);

あとは取得したResolveInfoのプロパティからいろんな情報が取得可能。
loadLabel(pm)で取得→アプリケーションのタイトル
applicationInfo.packageName→パッケージ名
activityInfo.name→アクティビティ名
activityInfo.loadIcon(pm)で取得→icon画像
http://d.hatena.ne.jp/sy-2010/20100202/1265101698

まとめると

まず、アクション種別を指定して、アクティビティかたまりの情報を取得する。
そのあと、アクティビティごとに必要情報を取得。

// パッケージマネージャを利用
PackageManager pm = getPackageManager();

// インテントで取得するアクティビティ種類を指定
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

// 該当するアクティビティの情報たちを取得
List<ResolveInfo> appsInfo = pm.queryIntentActivities(intent, 0);

// それぞれのアクティビティ内の取得する情報を指定
for(int i = 0; i < appsInfo.size(); i++) {
  ResolveInfo info = appsInfo.get(i);
  ActivityInfo activityinfo = info.activityInfo;

  // アプリ内の取得する情報を取得
  sb.append(activityinfo.packageName + "\n");
}