如何获取应用程序图标?

时间:2018-02-17 14:21:11

标签: android listview android-package-managers

我尝试使用所有已安装的应用程序的名称,权限和图标来创建应用程序。 我已经成功完成了第一次和第二次,但我不知道该怎么做第三次。

这是AppsAdapter类:

class AppsAdapter extends BaseAdapter {

private final Context mContext;
private List<Pair<String, List<String>>> mAppsWithPermission;

public AppsAdapter(Context context, List<Pair<String, List<String>>> appsWithPermission) {
    mContext = context;
    mAppsWithPermission = appsWithPermission;
}

static class ViewHolder {
    public TextView appName;
    public TextView appPermissions;
    public ImageView appIcon;
}

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

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

@Override
public long getItemId(int position) {
    return mAppsWithPermission.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {

        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.appName = convertView.findViewById(R.id.list_item_appname);
        holder.appPermissions = convertView.findViewById(R.id.list_item_apppermissions);
        holder.appIcon = convertView.findViewById(R.id.list_item_appicon);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Pair<String, List<String>> item = mAppsWithPermission.get(position);

    holder.appName.setText(item.first);
    holder.appPermissions.setText(item.second.toString());


    return convertView;

}
}

MainActivity:

public class MainActivity extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final PackageManager packageManager = getPackageManager();

    final ApplicationInfo packageNames = new ApplicationInfo();

    final List<Pair<String, List<String>>> appsWithPermission = new ArrayList<>();

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
    for (ResolveInfo info : apps) {
        final ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
        final String appName = (String) applicationInfo.loadLabel(packageManager);

        final List<String> permissions = new ArrayList<>();

        if (appName != null) {

            try {
                final PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
                final String[] requestedPermissions = packageInfo.requestedPermissions;

                if (requestedPermissions != null) {
                    permissions.addAll(Arrays.asList(requestedPermissions));
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        appsWithPermission.add(new Pair<>(appName, permissions));
    }

    final ListView listView = findViewById(R.id.list_view);
    final AppsAdapter appsAdapter = new AppsAdapter(this, appsWithPermission);
    listView.setAdapter(appsAdapter);

}
}

我还上传了更新的MainActivity。

我试图获取应用图标。

我该怎么办?

提前致谢

Faby

2 个答案:

答案 0 :(得分:2)

我建议重构你的适配器来保存app.get('/hello', (req, res) => { controller.doSomthingAsync(req).then(() => { // Return removed res.send({message: "Hello World"}); }) }); 。在应用程序的某个地方,您可以调用List<PackageItemInfo>,它会为您提供PackageManager.getInstalledPackages个对象的列表 - 将其传递给您的适配器。然后使用PackageItemInfo将相应的图标加载到PackageItemInfo.loadIcon

答案 1 :(得分:0)

试试这个

try {
      String pkg = "com.simple.app";//your package name
      Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
     imageView.setImageDrawable(icon);
   } catch (PackageManager.NameNotFoundException ne) {
}