Xamarin.Android インストールされているアプリケーションの列挙
1 アプリケーション列挙

パッケージマネージャで条件となるインテントを指定してQueryIntentActivities()を読みだすと、条件にヒットしたアプリケーションの一覧が列挙されます。
現在インストールされているアプリケーションを列挙するためには、その条件として下記の2つを指定します。
ACTION_MAIN(アプリケーションの起動のエントリーポイント)
CATEGORY_LAUNCHER(トップレベルのランチャーに表示されるべきもの)
そう、これは通常アプリのマニュフェストで指定しているintent-filterそのものです。
<activity android:icon="@drawable/icon" android:label="AndroidApplication1" android:name="androidapplication1.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
この条件を指定して列挙をかけた例が下記のとおりです。
//起動用アクティビティの定義 // Intent.ActionMain:起動可能なインテント var mainIntent = new Intent(Intent.ActionMain,null); // Intent.CategoryLauncher デスクトップから可能なインテント(通常アプリ) mainIntent.AddCategory(Intent.CategoryLauncher); //アプリ一覧の取得 var ar = PackageManager.QueryIntentActivities(mainIntent, 0); var list = new List<ListItem>(); foreach (var a in ar) { //アイコン var icon = a.ActivityInfo.LoadIcon(PackageManager); //アプリ名 var name = a.LoadLabel(PackageManager); //アクティビティ名 var fullName = a.ActivityInfo.Name; list.Add(new ListItem() { Icon = icon, Name = name, FullName = fullName }); } //リストビューへのアダプタ設定 var listView = FindViewById<ListView>(Resource.Id.listView1); listView.Adapter = new MyAdapter(this, 0, list);
2 アプリケーションの管理(詳細)画面

次に、リストビューをクリックした際に、アプリケーションの管理(詳細)画面に遷移する例です。
SetDataでアプリケーションのパッケージ名、SetComponentでアプリケーション管理の詳細画面を指定したインテントでアクティビティを起動しています。
詳細画面では、アプリのアンインストールや、デフォルト設定などを行うことができます。
//リストビュー(アイテム)をクリックした際のイベントを定義 listView.ItemClick += (s, a) =>{ var intent = new Intent(); //アプリケーションのパッケージ名 var fullName = ar[a.Position].ActivityInfo.PackageName; intent.SetData(Android.Net.Uri.FromParts("package", fullName, null)); //アプリケーション管理の詳細画面 var cn = ComponentName.UnflattenFromString("com.android.settings/.applications.InstalledAppDetails"); intent.SetComponent(cn); //明示的アクティビティの起動 StartActivity(intent); };
3 AdapterList
参考に、今回リストビューで使用したアダプタのコードです。
Xamarin.Android ListViewの2.表示内容のカスタマイズを少し修正しただけのものです
public class ListItem { public Drawable Icon; //アイコン public string Name; //名前 public string FullName; //アクティビティ名 } public class MyAdapter : ArrayAdapter<ListItem> { private readonly LayoutInflater _layoutInflater; public MyAdapter(Context context, int rid, IList<ListItem> list) : base(context, rid, list) { _layoutInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); } public override View GetView(int position, View convertView, ViewGroup parent) { //データを取り出す var item = GetItem(position); //レイアウトファイルからViewを作成 var view = _layoutInflater.Inflate(Resource.Layout.list_item, null); //アイコン var icon = view.FindViewById<ImageView>(Resource.Id.imageView); icon.SetImageDrawable(item.Icon); //名前 view.FindViewById<TextView>(Resource.Id.textViewName).Text = item.Name; //アクティビティ名 view.FindViewById<TextView>(Resource.Id.textViewFullName).Text = item.FullName; return view; } }