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建立适配器的视图:datas; public DemoGridAdapter(Context context, List 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 Listdatas; 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 appList = getAllApps(PackageInfoDemo.this); datas = new ArrayList (); 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 getAllApps(Context context) { List apps = new ArrayList (); PackageManager pManager = context.getPackageManager(); // 获取手机内全部应用 List 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 Listdatas; 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这里getDrawableForDensity须要是15版本号以上的SDK支持。全部低版本号的Launcher不能够使用;datas = new ArrayList (); 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 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 getDatas() { loadAllAppsByBatch(); return datas; }}
本博文使用的两种方法都不须要配置不论什么权限。
Android4.2的Launcher中使用ResolveInfo进行获取所用的应用列表,这里的ResolveInfo的Demo也是从Launcher源代码中抄出来的;
也许还有第3种方法.......待续;
本文来自于CSDN博客,转载请联系作者;
注明出处
版权声明:本文博客原创文章,博客,未经同意,不得转载。