1.系统应用程序界面
获取系统程序ApplicationInfo集合
private void loadApplications(boolean isLaunching) { if (isLaunching && mApplications != null) { return; } PackageManager manager = getPackageManager(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0); Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager)); if (apps != null) { final int count = apps.size(); if (mApplications == null) { mApplications = new ArrayList<ApplicationInfo>(count); } mApplications.clear(); for (int i = 0; i < count; i++) { ApplicationInfo application = new ApplicationInfo(); ResolveInfo info = apps.get(i); application.title = info.loadLabel(manager); application.setActivity(new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name), Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); application.icon = info.activityInfo.loadIcon(manager); mApplications.add(application); } } }
程序列表构造器
private class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> { private Rect mOldBounds = new Rect(); public ApplicationsAdapter(Context context, ArrayList<ApplicationInfo> apps) { super(context, 0, apps); } @Override public View getView(int position, View convertView, ViewGroup parent) { final ApplicationInfo info = mApplications.get(position); if (convertView == null) { final LayoutInflater inflater = getLayoutInflater(); convertView = inflater.inflate(R.layout.application, parent, false); } Drawable icon = info.icon; if (!info.filtered) { int width = 42;// (int) int height = 42;// (int) final int iconWidth = icon.getIntrinsicWidth(); final int iconHeight = icon.getIntrinsicHeight(); if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); } if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) { final float ratio = (float) iconWidth / iconHeight; if (iconWidth > iconHeight) { height = (int) (width / ratio); } else if (iconHeight > iconWidth) { width = (int) (height * ratio); } final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; final Bitmap thumb = Bitmap.createBitmap(width, height, c); final Canvas canvas = new Canvas(thumb); canvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 0)); mOldBounds.set(icon.getBounds()); icon.setBounds(0, 0, width, height); icon.draw(canvas); icon.setBounds(mOldBounds); icon = info.icon = new BitmapDrawable(thumb); info.filtered = true; } } final TextView textView = (TextView) convertView.findViewById(R.id.label); textView.setTextColor(Color.BLACK); if (color != null && color.length() == 10) { int col_int = ByteHelper.string2color(color); textView.setTextColor(col_int); }else { textView.setTextColor(Color.BLUE); } textView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null); textView.setText(info.title); return convertView; } }
添加构造,并设置监听
mGrid.setAdapter(new ApplicationsAdapter(Home_activity.this, mApplications)); mGrid.setSelection(0); mGrid.setOnItemClickListener(new ApplicationLauncher());
监听
private class ApplicationLauncher implements AdapterView.OnItemClickListener { public void onItemClick(AdapterView parent, View v, int position, long id) { ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); startActivity(app.intent); } }