25android:
id="@+id/app_title"
26android:
layout_width="wrap_content"
27android:
layout_height="wrap_content"
28/>
29
第四步:
也就是核心了,修改Launcher.java代码如下:
viewplaincopytoclipboardprint?
30packagecom.tutor.launcher;
31importjava.util.Collections;
32importjava.util.List;
33importandroid.app.Activity;
34importandroid.content.ComponentName;
35importandroid.content.Context;
36importandroid.content.Intent;
37importandroid.content.pm.PackageManager;
38importandroid.content.pm.ResolveInfo;
39importandroid.os.Bundle;
40importandroid.view.LayoutInflater;
41importandroid.view.View;
42importandroid.view.ViewGroup;
43importandroid.widget.AdapterView;
44importandroid.widget.BaseAdapter;
45importandroid.widget.GridView;
46importandroid.widget.ImageView;
47importandroid.widget.TextView;
48importandroid.widget.AdapterView.OnItemClickListener;
49publicclassLauncherextendsActivityimplementsOnItemClickListener{
50
51privateGridViewmGridView;
52privateContextmContext;
53privatePackageManagermPackageManager;
54privateListmAllApps;
55
56publicvoidonCreate(BundlesavedInstanceState){
57super.onCreate(savedInstanceState);
58setContentView(R.layout.main);
59
60setupViews();
61
62}
63
64publicvoidsetupViews(){
65mContext=Launcher.this;
66mPackageManager=getPackageManager();
67mGridView=(GridView)findViewById(R.id.allapps);
68bindAllApps();
69
70mGridView.setAdapter(newGridItemAdapter(mContext,mAllApps));
71mGridView.setNumColumns(4);
72mGridView.setOnItemClickListener(this);
73}
74
75publicvoidbindAllApps(){
76//这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性
77//也就是应用的入口
78IntentmainIntent=newIntent(Intent.ACTION_MAIN,null);
79mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
80//符合上面条件的全部查出来,并且排序
81mAllApps=mPackageManager.queryIntentActivities(mainIntent,0);
82Collections.sort(mAllApps,newResolveInfo.DisplayNameComparator(mPackageManager));
83}
84
85
86
87//gridview点击事件,点击进入相关应用
88@Override
89publicvoidonItemClick(AdapterView
>parent,Viewview,intposition,longid){
90//TODOAuto-generatedmethodstub
91ResolveInfores=mAllApps.get(position);
92//该应用的包名和主Activity
93Stringpkg=res.activityInfo.packageName;
94Stringcls=res.activityInfo.name;
95
96ComponentNamecomponet=newComponentName(pkg,cls);
97
98Intenti=newIntent();
99i.setComponent(componet);
100startActivity(i);
101}
102
103//不明白BaseAdapter的用法我高手进阶里有
104privateclassGridItemAdapterextendsBaseAdapter{
105privateContextcontext;
106privateListresInfo;
107
108//构造函数
109publicGridItemAdapter(Contextc,Listres){
110context=c;
111resInfo=res;
112}
113@Override
114publicintgetCount(){
115//TODOAuto-generatedmethodstub
116returnresInfo.size();
117}
118@Override
119publicObjectgetItem(intposition){
120//TODOAuto-generatedmethodstub
121returnnull;
122}
123@Override
124publiclonggetItemId(intposition){
125//TODOAuto-generatedmethodstub
126return0;
127}
128@Override
129publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
130
131//不明白LayoutInflater的我android高手进阶里有
132convertView=LayoutInflater.from(context)
133.inflate(R.layout.application_layout,null);
134
135ImageViewapp_icon=(ImageView)convertView.findViewById(R.id.app_icon);
136TextViewapp_tilte=(TextView)convertView.findViewById(R.id.app_title);
137
138ResolveInfores=resInfo.get(position);
139app_icon.setImageDrawable(res.loadIcon(mPackageManager));
140app_tilte.setText(res.loadLabel(mPackageManager).toString());
141returnconvertView;
142}
143
144}
145}
第五步:
运行以上工程,得到的效果图如下:
图1.列出所有安装的应用.
图2.点击aQQ应用进入到相应的应用里。
OK,今天就写到这里,大家有什么不明白的地方可以留言。
thx~