Angelika
Angelika

Reputation: 381

How to use getPackageManager?

In order to show installed applications on the Android emulator, I tried this code. It compiles successfully but doesn't work. What's wrong?

package pack.GetAllInstalledApplications;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

 public class GetAllInstalledApplicationsExample extends Activity {

 public  ArrayList <PackageInfoStruct> res = new ArrayList <PackageInfoStruct>();

 public ListView list;
 public String app_labels[];


 public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getPackages();

        list = (ListView)findViewById(R.id.ListView01);
        try{
            list.setAdapter(new ArrayAdapter<String>         (this,android.R.layout.simple_dropdown_item_1line,app_labels));
        }catch(Exception e){
            System.out.println("Err ++> " + e.getMessage());
               Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }

 }
private ArrayList
 <PackageInfoStruct> getPackages() {
    ArrayList
 <PackageInfoStruct> apps = getInstalledApps(false);
    final int max = apps.size();
    for (int i=0; i < max; i++) {
        apps.get(i);
    }
    return apps;
}

private ArrayList
  <PackageInfoStruct> getInstalledApps(boolean getSysPackages) {

    List
  <PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    try{
        app_labels = new String[packs.size()];
    }catch(Exception e){
                   Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
    }
    for(int i=0;i < packs.size();i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        PackageInfoStruct newInfo = new PackageInfoStruct();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(newInfo);

        app_labels[i] = newInfo.appname;
    }
    return res;
}
    }  
  class PackageInfoStruct {             
 String appname = "";           
 String pname = "";             
 String versionName = "";      
 int versionCode = 0;           
 Drawable icon;                 
  }

Upvotes: 0

Views: 9247

Answers (3)

cmh
cmh

Reputation: 271

I change the adapter layout, use the android.R.layout.simple_list_item_1 on your code, and it works.

Upvotes: 0

gwvatieri
gwvatieri

Reputation: 5183

Before rendering the app labels you should add some log in order to understand if you are actually retrieving the apps. Verified that then you can deal with the adapter.

Remember that the ArrayAdapter's constructor get as second parameter the resource id of a text view (otherwise it crashes), so make sure of that.

Moreover, for discarding the system package I use this line of code:

... if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) continue; ...

where pi is the PackageInfo.

Let me know if it works

Upvotes: 0

ThomasW
ThomasW

Reputation: 17317

In your call to list.setAdapter you should probably use a different layout. I tried simple_list_item_1

list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, app_labels));

and it appears to then display the items correctly.

Upvotes: 0

Related Questions