2010年3月19日金曜日

Android 特定の Intent (Action) を処理できる Activity (アプリ)の一覧を取得

なんか見つからなかったので作りました。

android.content.pm パッケージの
PackageManagerクラスの
 "queryIntentActivities (Intent intent, int flags)"
で取得できます。

こんな感じ

  1. PackageManager pm = getPackageManager();  
  2.   
  3. List<resolveinfo> resolveInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_SEARCH), PackageManager.MATCH_DEFAULT_ONLY);  
  4.   
  5. StringBuffer sb = new StringBuffer();  
  6.                
  7. for(int i = 0; i < resolveInfo.size(); i++) {  
  8.   ResolveInfo info = resolveInfo.get(i);  
  9.   ActivityInfo activityinfo = info.activityInfo;  
  10.   
  11.   sb.append(activityinfo.packageName + "\n");  
  12. }          
  13. textView.setText(sb.toString());    
  14. </resolveinfo>  


ACTION_SEND などは、setType() しないと
パッケージが引っかかりません。
なので、こんな感じで
  1. PackageManager pm = getPackageManager();  
  2.   
  3. Intent intent = new Intent(Intent.ACTION_SEND);  
  4. intent.setType("text/plain");  
  5.   
  6. List<resolveinfo> resolveInfo = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  
  7. </resolveinfo>  


さらにさらに、
 とってきた activity の一覧をソートするには
 "Collections" と "ResolveInfo.DisplayNameComparator"
 を使います。

こんな感じ
  1. // sort  
  2. Collections.sort(resolveInfo, new ResolveInfo.DisplayNameComparator(pm));                


Inflator を使って TableLayout にしました。

ソースとアプリも公開しちゃいます。
GetIntentList.apk

GetIntentList.zip


参考ページ
http://developer.android.com/intl/ja/reference/android/content/pm/PackageManager.html#queryIntentActivities(android.content.Intent, int)
http://developer.android.com/intl/ja/guide/topics/intents/intents-filters.html#imatch

0 件のコメント:

コメントを投稿