android - 如何获取已安装的应用程序图标并使其可用于 gridView

标签 android listview android-listview

我正在尝试在用户安装的应用程序列表中获取已安装的应用程序图标作为复选框,如下所示:

        addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if (addCheckbox.isChecked()){
                   System.out.println("Checked");
// GET ITEM ICON HERE
               }else{
                   System.out.println("Un-Checked");
               }

           }});

在我的列表中,我为每个 listView 项目进行了这样的设置:

<?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="?android:attr/listPreferredItemHeight"
android:padding="5dip" 
 >

<ImageView
  android:id="@+id/ivIcon"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginRight="5dip"
  android:scaleType="center"
  android:contentDescription="@string/desc"
/>

<LinearLayout
  android:orientation="vertical"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:layout_weight="1"    
>

  <TextView
      android:id="@+id/tvName"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center_vertical"         
  />

  <TextView
      android:id="@+id/tvPack"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:singleLine="true"
      android:ellipsize="marquee"         
  />

</LinearLayout>

<CheckBox
  android:id="@+id/addCheckbox"
  android:layout_width="0dp"
  android:layout_height="fill_parent"
  android:layout_weight="0.3"
  android:gravity="center_vertical" 
/>

</LinearLayout>

所以我尝试通过这样做来获取 listView 项目,然后获取图标(就像我在其他类(class)中所做的那样):

        // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }

    });

但我不知道如何实现它,以便我可以从列表中获取所选项目,以便我可以获得图标(如果我什至需要这样做,或者如果我可以使用我的集合中的图标) listView 项目的上方)。

我正在尝试获取图标,以便可以在 gridView 中使用它。

我的处理方式正确吗?否则我该怎么办? (如果您需要查看更多代码,请告诉我!)

进一步说明:

因此,为了获取已安装应用程序的列表,我有一个类来获取列表的信息,然后是一个自定义适配器。这是 Utlities.java (我获取信息的部分):

public class Utilities {

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<?> getInstalledApplication(Context c) {
    return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}

然后我这里有一个自定义适配器,它根据布局对信息进行排序: AppInfoAdapter.java:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter; 

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    }

@Override
public int getCount() {
    return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
    return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // get the selected entry
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
    final CheckBox addCheckbox = (CheckBox)v.findViewById(R.id.addCheckbox);

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));
    tvPkgName.setText(entry.packageName);
    addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if (addCheckbox.isChecked()){
                                       System.out.println("Checked");
                   PackageManager pm = mContext.getPackageManager();
                   Drawable icon = pm.getApplicationIcon(apk.package_name);
                   Drawable default_icon = pm.getDefaultActivityIcon();
                   if (icon instanceof BitmapDrawable && default_icon instanceof BitmapDrawable) {
                       BitmapDrawable icon_bd = (BitmapDrawable)icon;
                       Bitmap icon_b = icon_bd.getBitmap();
                       BitmapDrawable default_bd = (BitmapDrawable)pm.getDefaultActivityIcon();
                       Bitmap default_b = default_bd.getBitmap();
                       if (icon_b == default_b) {
                           // It's the default icon
                       }
               }else{
                   System.out.println("Un-Checked");
               }

           }});


    // return view
    return v;
}

所以在这里,我必须修复这些行:

Drawable icon = pm.getApplicationIcon(apk.package_name);

这样我就可以通过包名称获取图标。这就是为什么我尝试在“就像我在其他类(上面)中所做的那样”部分中导入和使用,以便我可以获得 listView 项目的位置,然后获取图标。

注意:我尝试过这样的事情:

Drawable icon = pm.getApplicationIcon(AppInfoAdapter.getItem(position).packageName);

但它不起作用,只会导致一堆错误。

新错误:

当我更改这些行时:

        ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));

对此:

        ivAppIcon.setImageDrawable(entry.applicationInfo.loadIcon(mPackManager));
    tvAppName.setText(entry.applicationInfo.loadLabel(mPackManager));

我在这里遇到错误:

                            Drawable icon = pm
                                .getApplicationIcon(entry.packageName);

说用 try 和 catch 语句包围它,所以我这样做:

Drawable icon = null;
                        try {
                            icon = pm
                                    .getApplicationIcon(entry.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

最佳答案

我不完全确定您要做什么,但我知道您不知道如何获取另一个应用程序的图标。快速谷歌搜索让我找到了这段代码:

PackageManager manager = mContext.getPackageManager();
Drawable appIcon = manager.getApplicationIcon("com.google.maps");

我认为这会做你想做的事情 - 然后你在 gridview 中使用可绘制对象(这是我不清楚的问题的一部分)

编辑

好的,我已经更仔细地了解了您现在所做的事情。您正在使用

获取应用程序列表
getInstalledApplications(PackageManager.GET_META_DATA)

根据this ,这将返回 List<ApplicationInfo>在这种情况下,这不一定是您想要的。 This问题询问 ApplicationInfo 和 PackageInfo 之间的区别是什么,第一个答案告诉我“ApplicationInfo 实际上是 PackageInfo 的字段/属性”。所以现在我知道,通过限制你必须使用的信息,你会立即让自己的事情变得更加困难。相反,我会打电话 getInstalledPackages这将返回 List<PackageInfo 。由此,如果您稍后需要,您可以通过获取 applicationInfo 来获取当前拥有的相同信息。 field 。我将按以下方式更改代码:

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<PackageInfo> getInstalledApplications(Context c) {
return c.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

}

并更改 ApplicationInfo 的所有实例至PackageInfo在 AppInfoAdapter 中。例如...

// get the selected entry
PackageInfo entry = (PackageInfo) mListAppInfo.get(position);

然后就可以替换

Drawable icon = pm.getApplicationIcon(apk.package_name);

Drawable icon = pm.getApplicationIcon(entry.packageName);

看看进展如何...

关于android - 如何获取已安装的应用程序图标并使其可用于 gridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19600754/

相关文章:

android - FCM android onmessagereceived 未调用

java - 由 : java. lang.NumberFormatException 引起:无效的 int: "9.9"

android - 查看生成的ListView时间

java - 带有开关按钮的 Android ListView - 一次只有一个开关处于 Activity 状态

android - 在 Android 中使用 Pull to refresh 刷新 Listview 后获取添加的新项目

android - 我如何在 React Native 中提供多设备支持?

android - onTextChanged 在自定义 EditText 中无法正常工作

android - 刷新 ListView 中的特定项目

asp :ListView 宽度的 CSS

Android 的 ArrayAdapter - 添加字符串 ListView 而不是替换