Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use getPackageManager?
    text
    copied!<p>In order to show installed applications on the Android emulator, I tried this code. It compiles successfully but doesn't work. What's wrong?</p> <pre><code>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 &lt;PackageInfoStruct&gt; res = new ArrayList &lt;PackageInfoStruct&gt;(); 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&lt;String&gt; (this,android.R.layout.simple_dropdown_item_1line,app_labels)); }catch(Exception e){ System.out.println("Err ++&gt; " + e.getMessage()); Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); } } private ArrayList &lt;PackageInfoStruct&gt; getPackages() { ArrayList &lt;PackageInfoStruct&gt; apps = getInstalledApps(false); final int max = apps.size(); for (int i=0; i &lt; max; i++) { apps.get(i); } return apps; } private ArrayList &lt;PackageInfoStruct&gt; getInstalledApps(boolean getSysPackages) { List &lt;PackageInfo&gt; 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 &lt; packs.size();i++) { PackageInfo p = packs.get(i); if ((!getSysPackages) &amp;&amp; (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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload