2

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

        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 项,然后获取图标(就像我在其他班级中所做的那样):

        // 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);
        }

    });

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

我正在尝试获取图标,以便可以在 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 and catch语句包围它,所以我这样做:

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

1 回答 1

3

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

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

我认为这会做你想做的事 - 然后你在你的gridview中使用drawable(这是你问题的一部分,我不清楚)

编辑

好的,我已经更仔细地查看了您正在做的事情。您正在获取使用的应用程序列表

getInstalledApplications(PackageManager.GET_META_DATA)

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

/*
 * 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);

}

并将 AppInfoAdapter 中的所有实例更改ApplicationInfoPackageInfo。例如...

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

然后你可以更换

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

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

看看情况如何...

于 2013-10-25T23:45:23.963 回答