[Xamarin] 取得所有已安裝軟體清單 (转帖)

最近會用到,簡單記錄一下,抓取所有該手機已經安裝的軟體清單

結果圖:

shot_000007


首先介紹一下Layout :  \Resources\Layout\Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnGetAllList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取得所有APPS" />
    <ScrollView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView1">
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1" />
    </ScrollView>
</LinearLayout>

預覽:
2013-09-17_152410

按下按鈕(btnGetAllList)後 會將所有安裝過的軟體名稱放入至按鈕下方的LinearLayout(linearLayout1)中,因為怕很多所以有用ScrollViewer包起來

接下來就是 C# Code 部分:

using Android.App;
using Android.Content.PM;
using Android.Graphics;
using Android.Widget;
using Android.OS;
 
namespace GetAllAppList
{
    [Activity(Label = "取得所有app清單", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
 
            var btnGetAllList = FindViewById<Button>(Resource.Id.btnGetAllList);
            var appContainer = FindViewById<LinearLayout>(Resource.Id.linearLayout1);
 
 
            //取得所有APPS 按鈕事件
            btnGetAllList.Click += delegate
                {
 
                    var appLists = PackageManager.GetInstalledPackages(PackageInfoFlags.Activities);
                    Toast.MakeText(this, "發現已安裝數量:" + appLists.Count + "", ToastLength.Long).Show();
 
                    //清空容器
                    appContainer.RemoveAllViews();
 
 
                    foreach (var resolveInfo in appLists)
                    {
                        var t = new TextView(this);
                        //設定顏色 看起來比較geek
                        t.SetTextColor(new Color(0, 255, 0));
                        t.Text = resolveInfo.PackageName;
                        appContainer.AddView(t);
                    }
                };
        }
    }
}
 

重點在這一行:

var appLists = PackageManager.GetInstalledPackages(PackageInfoFlags.Activities);

結果預覽:
shot_000006

shot_000007

posted on 2013-11-27 09:34  王员外王爷  阅读(239)  评论(0编辑  收藏  举报

导航