Launcher最基本的是让所有的应用程序和入口图标的列表。有两种方法来获得,一般:

PackageInfo

ResolveInfo

执行获取全部APP的Launcher而且同意进行点击事件,进入到应用

以下通过这两种方法获取到全部应用的列表:

建立基本数据:

PakageMod.java

public class PakageMod {

	public String pakageName;
public String appName;
public Drawable icon; public PakageMod() {
super();
} public PakageMod(String pakageName, String appName, Drawable icon) {
super();
this.pakageName = pakageName;
this.appName = appName;
this.icon = icon;
}
}

建立适配器:

public class DemoGridAdapter extends BaseAdapter {

	private LayoutInflater inflater;
private List<PakageMod> datas; public DemoGridAdapter(Context context, List<PakageMod> datas) {
super();
inflater = LayoutInflater.from(context);
this.datas = datas;
} @Override
public int getCount() {
return datas.size();
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// 使用View的对象itemView与R.layout.item关联
convertView = inflater.inflate(R.layout.apps, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.apps_image);
holder.label = (TextView) convertView
.findViewById(R.id.apps_textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
} holder.icon.setImageDrawable(datas.get(position).icon);
holder.label.setText(datas.get(position).appName); return convertView; } class ViewHolder {
private ImageView icon;
private TextView label;
}
}

建立适配器的视图:

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/apps_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:icon="@drawable/ic_launcher" /> <TextView
android:id="@+id/apps_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="5"
android:maxLines="1"
android:text="good" /> </LinearLayout>

以下在Activity中获取到列表并显示到GridView中,并点击进入应用;

使用PackageInfo

public class PackageInfoDemo extends Activity {

	private GridView gridview;
private PackageManager pManager;
private List<PakageMod> datas;
private String tag = "MainActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
// 取得gridview
gridview = (GridView) findViewById(R.id.gridview);
// 获取图片、应用名、包名
pManager = PackageInfoDemo.this.getPackageManager();
List<PackageInfo> appList = getAllApps(PackageInfoDemo.this);
datas = new ArrayList<PakageMod>();
for (int i = 0; i < appList.size(); i++) {
PackageInfo pinfo = appList.get(i);
PakageMod shareItem = new PakageMod();
// 设置图片
shareItem.icon = pManager.getApplicationIcon(pinfo.applicationInfo);
// 设置应用程序名字
shareItem.appName = pManager.getApplicationLabel(
pinfo.applicationInfo).toString();
// 设置应用程序的包名
shareItem.pakageName = pinfo.applicationInfo.packageName; datas.add(shareItem); }
gridview.setAdapter(new baseAdapter(this, datas)); // 点击应用图标时,做出响应
gridview.setOnItemClickListener(new ClickListener());
} public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内全部应用
List<PackageInfo> packlist = pManager.getInstalledPackages(0);
for (int i = 0; i < packlist.size(); i++) {
PackageInfo pak = (PackageInfo) packlist.get(i);
// if()里的值假设<=0则为自己装的程序。否则为系统project自带
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// 加入自己已经安装的应用程序
// apps.add(pak);
}
apps.add(pak);
}
return apps;
} private class ClickListener implements OnItemClickListener { @Override
public void onItemClick(AdapterView<? > arg0, View arg1, int position,
long arg3) {
Intent intent = new Intent();
intent = PackageInfoDemo.this.getPackageManager()
.getLaunchIntentForPackage(datas.get(position).pakageName);
startActivity(intent);
}
}
}

使用ResolveInfo

public class ResolveInfoDemo extends Activity {
private GridView gridview;
private PackageManager pManager;
private List<PakageMod> datas;
private String tag = "ResolveInfoDemo"; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
// 取得gridview
gridview = (GridView) findViewById(R.id.gridview); // 获取图片、应用名、包名
pManager = this.getPackageManager(); datas = new GetAllApps(this).getDatas();
gridview.setAdapter(new DemoGridAdapter(this, datas));
gridview.setOnItemClickListener(new ClickListener());
} // 当用户点击应用程序图标时,将对这个类做出响应
private class ClickListener implements OnItemClickListener { @Override
public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
long arg3) {
Intent intent = new Intent();
intent = getPackageManager().getLaunchIntentForPackage(
datas.get(arg2).pakageName);
startActivity(intent);
} }
}

GetAllApps.java

public class GetAllApps {

	private Context mContext;
private PackageManager packageManager;
private int mIconDpi;
private String tag = "GetAllApps";
private List<PakageMod> datas = new ArrayList<PakageMod>(); public GetAllApps(Context mContext){
this.mContext = mContext;
ActivityManager activityManager =
(ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
packageManager = mContext.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
} public void loadAllAppsByBatch() {
List<ResolveInfo> apps = null;
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
apps = packageManager.queryIntentActivities(mainIntent, 0);
for (int i = 0; i < apps.size(); i++) {
String packageName = apps.get(i).activityInfo.applicationInfo.packageName;
String title = apps.get(i).loadLabel(packageManager).toString();
Drawable icon = null;
if(title == null){
title = apps.get(i).activityInfo.name;
}
ActivityInfo info = apps.get(i).activityInfo;
icon = getFullResIcon(info);
datas.add(new PakageMod(packageName,title,icon));
}
} public Drawable getFullResIcon(ActivityInfo info) {
Resources resources;
try {
resources = packageManager.getResourcesForApplication(
info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
int iconId = info.getIconResource();
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
} public Drawable getFullResDefaultActivityIcon() {
return getFullResIcon(Resources.getSystem(),
android.R.mipmap.sym_def_app_icon);
} public Drawable getFullResIcon(Resources resources, int iconId) {
Drawable d;
try {
// requires API level 15 (current min is 14):
d = resources.getDrawableForDensity(iconId, mIconDpi);
} catch (Resources.NotFoundException e) {
d = null;
} return (d != null) ? d : getFullResDefaultActivityIcon();
} public List<PakageMod> getDatas() {
loadAllAppsByBatch();
return datas;
}
}

这里getDrawableForDensity须要是15版本号以上的SDK支持。全部低版本号的Launcher不能够使用;

本博文使用的两种方法都不须要配置不论什么权限。

Android4.2的Launcher中使用ResolveInfo进行获取所用的应用列表,这里的ResolveInfo的Demo也是从Launcher源代码中抄出来的;

也许还有第3种方法.......待续;

本文来自于CSDN博客,转载请联系作者;

注明出处http://blog.csdn.net/dreamintheworld/article/details/39718581

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. UVA 11210 中国麻将
  2. lua闭合函数
  3. python学习之路-day12-mysql &amp;&amp; orm
  4. 浅论ViewController的加载 -- 解决 viewDidLoad 被提前加载的问题(pushViewController 前执行)
  5. iOS深入学习(再谈block)
  6. mysql 检查字符串是否包含子串
  7. Es索引优化
  8. DEEP LEARNING IS THE FUTURE: Q&amp;A WITH NAVEEN RAO OF NERVANA SYSTEMS
  9. vim插件管理之Vundle
  10. 转: 第二章 IoC Annotation注入
  11. Date 类 02
  12. PythonStudy——运算符优先级 Operator precedence
  13. ARMV8 datasheet学习笔记4:AArch64系统级体系结构之存储模型
  14. 002-linux——控制台的使用:
  15. WLC5520无法通过无线客户端进行网管故障解决
  16. 使用Speech SDK 5.1文字转音频
  17. Oracle to_date()函数的用法介绍
  18. Linux下搭建gtk+2.0开发环境
  19. 处于同一域中的两台SQL Server 实例无法连接
  20. MATLAB 条形图或饼状图 图案填充

热门文章

  1. Opencv分水岭算法——watershed自动图像分割用法
  2. 【37.38%】【codeforces 722C】Destroying Array
  3. JAVA中try-catch异常逃逸
  4. [Angular2 Router] Get activated router url
  5. win7
  6. 【30.49%】【codeforces 569A】Music
  7. 【codeforces 762A】k-th divisor
  8. python启动应用程序和终止应用程序
  9. 小强的HTML5移动开发之路(27)—— JavaScript回顾2
  10. WPF随笔(九)--使用路径动画模拟管道流体流向