Android---获取已安装应用列表


我们手机上安装的应用默认是以GridView的形式排放的,本篇博客将讲述以ListView的形式获得所有应用,并且点击应用后可以启动。

1.
ListView item的布局文件 list_item.xml


<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="48dp"
  android:padding="5dip" 
  >   
  <ImageView
      android:id="@+id/ivIcon"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_marginRight="5dip"
      android:scaleType="center"/>

  <LinearLayout
      android:orientation="vertical"
      android:layout_width="0dip"
      android:layout_height="match_parent"
      android:layout_weight="1">   
      <TextView
          android:id="@+id/tvName"
          android:layout_width="match_parent"
          android:layout_height="0dip"
          android:layout_weight="1"
          android:gravity="center_vertical"/>
      <TextView
          android:id="@+id/tvPack"
          android:layout_width="match_parent"
          android:layout_height="0dip"
          android:layout_weight="1"
          android:singleLine="true"
          android:ellipsize="marquee"/>
  LinearLayout>

LinearLayout>

2.主布局文件activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ListView
        android:id="@+id/lvApps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
LinearLayout>

3.ListView的适配器AppInfoAdapter.java

public class AppInfoAdapter extends BaseAdapter {

    private Context mContext;
    private List mListApp;
    private LayoutInflater mLayoutInflater;
    private PackageManager mPackageManager;

    public AppInfoAdapter(Context mContext, List mListApp, PackageManager mPackageManager) {
        this.mContext = mContext;
        this.mListApp = mListApp;
        this.mPackageManager = mPackageManager;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mListApp.size();
    }

    @Override
    public Object getItem(int position) {
        return mListApp.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ApplicationInfo appInfo = (ApplicationInfo) mListApp.get(position);
        ViewHolder holder = null;
        if(convertView == null){

            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.list_item, null);
            holder.ivAppIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            holder.tvAppName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvPackageName = (TextView) convertView.findViewById(R.id.tvPack);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ivAppIcon.setImageDrawable(appInfo.loadIcon(mPackageManager));
        holder.tvAppName.setText(appInfo.loadLabel(mPackageManager));
        holder.tvPackageName.setText(appInfo.packageName);

        return convertView;
    }

    static class ViewHolder{
        ImageView ivAppIcon;
        TextView tvAppName;
        TextView tvPackageName;
    }
}

4.工具类 Utils.java

public class Utils {

    /**
     * 获取已安装的应用
     * @param context
     * @return
     */
    public static List getInstalledApp(Context context){
        return context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
    }

    /**
     * 根据包名,启动应用
     * @param context
     * @param pm
     * @param packageName
     * @return
     */
    public static boolean launchApp(Context context,PackageManager pm,String packageName){
        Intent intent = pm.getLaunchIntentForPackage(packageName);
        if(intent != null){
            try {
                context.startActivity(intent);
                return true;
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "Applicatin not found", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }

        return false;
    }
}

5.MainActivity.java

public class MainActivity extends Activity {

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.lvApps);
        AppInfoAdapter adapter = new AppInfoAdapter(this, Utils.getInstalledApp(this), getPackageManager());
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                ApplicationInfo info = (ApplicationInfo) parent.getItemAtPosition(position);
                Utils.launchApp(parent.getContext(), getPackageManager(), info.packageName);
            }
        });
    }
}

运行截图:
Android---获取已安装应用列表_第1张图片

源码获取

你可能感兴趣的:(Android开发简单应用,Android开发简单应用)