天天看點

簡單實作Android擷取已安裝APP清單清單顯示

activity代碼:

import android.app.ListActivity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.SimpleAdapter;

import com.mob.at.demo.util.AppInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AppListActivity extends ListActivity {

    private SimpleAdapter listItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_applist);
        initListView();
        this.setListAdapter(listItemAdapter);
    }
    
    private void initListView()   {

        PackageManager pm = getPackageManager();
        List<PackageInfo> installedPackages = pm.getInstalledPackages(0);  // 擷取是以已安裝的包
        ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
        ArrayList<AppInfo> list = new ArrayList<AppInfo>();
        for (PackageInfo packageInfo : installedPackages) {
            AppInfo info = new AppInfo();
            String packageName = packageInfo.packageName;  // 包名
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;  // 應用資訊
            String name = applicationInfo.loadLabel(pm).toString();  // 應用名稱
            Drawable icon = applicationInfo.loadIcon(pm);  // 應用圖示
            System.out.println("name=========="+name);
            System.out.println("packageName=========="+packageName);
            info.name = name;
            info.packageName = packageName;
            info.icon = icon;
            // 狀态機,通過01狀态來表示是否具備某些屬性和功能
            int flags = applicationInfo.flags;  // 擷取應用标記
            if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo
                    .FLAG_EXTERNAL_STORAGE) {
                //安裝在sdcard
                info.isRom = false;

            } else {
                //安裝在手機
                info.isRom = true;
            }

            if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo
                    .FLAG_SYSTEM) {
                //系統應用
                info.isUser = false;

            } else {
                //使用者應用
                info.isUser = true;
            }

            if (info.isUser) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("ItemTitle", name);    //文字
                map.put("ItemImage", icon);   //圖檔
                listItems.add(map);
            }

        }

        System.out.println("listItems=========="+listItems.size());

        listItemAdapter = new SimpleAdapter(this, listItems,
                R.layout.list_item,
                new String[] {"ItemTitle", "ItemImage"},
                new int[ ] {R.id.ItemTitle, R.id.ItemImage}
        );
    }

}
           
list_item布局檔案代碼:      
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentLeft="true"
        android:id="@+id/ItemImage" />
    <TextView
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:text="123"
        android:id="@+id/ItemTitle" />

</RelativeLayout>