获取在设备上运行的应用程序图标

时间:2012-03-30 04:23:15

标签: android listview icons

我有一个列表活动,显示设备中所有正在运行的应用程序。它显示所有应用程序的默认图标。但我需要为列表中的每个应用程序获取实际应用程序图标。

public class ApplicationList extends ListActivity {
DataHelper dh;
ImageView vi;
private Drawable icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.applist);
    dh = new DataHelper(getApplicationContext());

    PackageManager pm = this.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    Log.d("Test1", "INSERTING APP LIST TO SERVER DB");
     List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
     ArrayList<String> applist = new ArrayList<String>();

     for(ResolveInfo rInfo: list){

        applist.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString() );
    }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.installedapp, R.id.textView1, applist);
    setListAdapter(adapter);
}

How to get the icon of other applications (Android) 我试过这个链接,但我没有解决问题。所以任何人都可以帮助我。谢谢!

3 个答案:

答案 0 :(得分:7)

试试这个

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
 Drawable[] icons=new Drawable[packs.size];
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    icons[i]= p.applicationInfo.loadIcon(getPackageManager());
 }

答案 1 :(得分:4)

试试这个:

Icon icon = p.applicationInfo.loadIcon(getPackageManager());
imageView.setImageDrawable(icon);

答案 2 :(得分:0)

尝试这样做一个名为packageinformation的类:

公共类PackageInformation {

private Context mContext;

public  PackageInformation(Context context){
    mContext=context;   
}


class InfoObject {
public String appname = "";
public String pname = "";
public String versionName = "";
public int versionCode = 0;
public Drawable icon;


public void InfoObjectAggregatePrint() {//not used yet
    Log.v(appname,appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}

}        private ArrayList getPackages(){          ArrayList apps = getInstalledApps(false); / * false =没有系统包* /     final int max = apps.size();     for(int i = 0; i

 public ArrayList<InfoObject> getInstalledApps(boolean getSysPackages) {
ArrayList<InfoObject> res = new ArrayList<InfoObject>();        
List<PackageInfo> packs = mContext.getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    if ((!getSysPackages) && (p.versionName == null)) {
        continue ;
    }
    InfoObject newInfo = new InfoObject();
    newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
    newInfo.pname = p.packageName;
    newInfo.versionName = p.versionName;
    newInfo.versionCode = p.versionCode;
    newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
    res.add(newInfo);
}
return res; 
 }


 }

把它扔到某个地方然后现在从你工作的Activity类中访问信息:

  PackageInformation androidPackagesInfo=new PackageInformation(this);      
    ArrayList<InfoObject> appsData=androidPackagesInfo.getInstalledApps(true);


for (InfoObject info : appsData) {


            Toast.makeText(MainActivity.this, info.appname,2).show();
                Drawable somedrawable=info.icon;

}