PackageManager的使用

这个app可以把你手机上能启动的APP都放进去,效果图:

这里写图片描述

这里写图片描述

JAVA代码


package com.example.days11gridview0002;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private GridView gv=null;
    private List<ResolveInfo> mList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setData();
        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ResolveInfo info=mList.get(position);
                String packname=info.activityInfo.packageName;
                String classname=info.activityInfo.name;
                Intent intent=new Intent();
                ComponentName name=new ComponentName(packname,classname);
                intent.setComponent(name);
                startActivity(intent);
            }
        });
    }

    private void setData() {
        PackageManager pm=getPackageManager();
        Intent intent=new Intent();
        intent.setAction(Intent.ACTION_MAIN);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        mList=pm.queryIntentActivities(intent, 0);
        MyAdapter adapter=new MyAdapter();
        gv.setAdapter(adapter);

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            ViewHolder holder;

            if (convertView == null) {

                holder = new ViewHolder();

                view = ((LayoutInflater) (getSystemService(LAYOUT_INFLATER_SERVICE)))
                        .inflate(R.layout.list_item, null);

                holder.iv_img = (ImageView) view.findViewById(R.id.im_imgs);
                holder.txt_text = (TextView) view.findViewById(R.id.txt_text);

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


            ResolveInfo resolveInfo=mList.get(position);
            Drawable loain=resolveInfo.loadIcon(getPackageManager());
            String  loainLable=(String) resolveInfo.loadLabel(getPackageManager());
            holder.txt_text.setText(loainLable);
            holder.iv_img.setImageDrawable(loain);
            return view;
        }

    }
    class ViewHolder {
        ImageView iv_img;
        TextView txt_text;
    }
    private void initView() {
        gv=(GridView) findViewById(R.id.gv);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.days11gridview0002.MainActivity" >

    <GridView
        android:id="@+id/gv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchMode="columnWidth"
        android:numColumns="3"/>

</RelativeLayout>

list_item.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/im_imgs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/im_imgs"
        android:gravity="center_horizontal"
        android:text="@string/hello_world" />

</RelativeLayout>

如果图片大小不一样,布局会乱,待改进。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值