简单实现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 installedPackages = pm.getInstalledPackages(0);  // 获取所以已安装的包
        ArrayList> listItems = new ArrayList>();
        ArrayList list = new ArrayList();
        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 map = new HashMap();
                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布局文件代码:


    
    

 

你可能感兴趣的:(android)