我正在尝试在我的用户安装的应用程序列表中获取已安装的应用程序图标,以获取一个复选框,如下所示:
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);
}
});
但是我不知道如何实现它以便我可以从列表中获取所选项目以便我可以获取图标(如果我甚至需要这样做或者如果我可以使用我的集合中的图标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();
}
答案 0 :(得分:2)
我不完全确定你要做什么,但我知道你不知道如何获得另一个应用程序的图标。 Googling的一小部分让我看到了这段代码:
PackageManager manager = mContext.getPackageManager();
Drawable appIcon = manager.getApplicationIcon("com.google.maps");
我认为你会做你想做的事情 - 然后你在gridview中使用drawable(这是你的问题的一部分,我不清楚)
修改强>
好吧,我已经看过你现在正在做的更紧密的事情了。您正在使用获取应用程序列表getInstalledApplications(PackageManager.GET_META_DATA)
根据this,这将返回List<ApplicationInfo>
,这不一定是您在这种情况下想要的。 This问题询问ApplicationInfo和PackageInfo之间的区别是什么,第一个答案告诉我“ApplicationInfo实际上是PackageInfo的字段/属性”。所以现在我知道你通过限制你必须使用的信息立刻让事情变得更加困难。相反,我会致电getInstalledPackages
,它会返回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中将ApplicationInfo
的所有实例更改为PackageInfo
。例如......
// get the selected entry
PackageInfo entry = (PackageInfo) mListAppInfo.get(position);
然后你可以替换
Drawable icon = pm.getApplicationIcon(apk.package_name);
与
Drawable icon = pm.getApplicationIcon(entry.packageName);
看看情况如何......