how to retrieve all installed android applications

2019-02-16 02:08发布

Did my search on the Stackoverflow and Google before already but I could not find any answer that worked out for me as I get errors when using them or that there are certain codes/parts missing or, that there could be a need to install additional library files.

So basically what I want to do is to retrieve all the installed apps in the android phone and display it out but I have no clue as to how to go about doing it. I usually run into error regarding the "context" variable or that my xml files does not match the .java files in one way or another.

Would anyone be kind enough to give me the source codes for all the required files(e.g. xml, java, manifest, drawable, layout etc) in order to achieve the objective of retrieving all android apps? Help would be very much appreciated

2条回答
手持菜刀,她持情操
2楼-- · 2019-02-16 02:45

First create main.xml file as follows,

<?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="fill_parent"
    android:orientation="vertical" >

            <ListView
                android:id="@+id/list1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>

</LinearLayout>

This list will be used to populate the installed applications,

Now create the Activity class to fetch the installed applications,

public class AppList extends Activity {
    private ListView lView;
    private ArrayList results;
    List<ResolveInfo> list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            results = new ArrayList();
        lView = (ListView) findViewById(R.id.list1);
        PackageManager pm = this.getPackageManager();

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

        list = pm.queryIntentActivities(intent,
            PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo : list) {
            results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm)
                    .toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                    .loadLabel(pm).toString());
        }
        lView.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, results));
    }
}

Edit- If you want to open the application from the list, once you click it. You have to override the onItemClick method of the ListView and in that method, you have to pass the intent to open the selected application.

Have a look here,

These will most probably solve your issue.

查看更多
等我变得足够好
3楼-- · 2019-02-16 02:55
public class AppList extends Activity {
 private ListView lView;
 private ArrayList results = new ArrayList();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lView = (ListView) findViewById(R.id.list1);
  PackageManager pm = this.getPackageManager();

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

  List<ResolveInfo>  list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
  for (ResolveInfo rInfo : list) {
   results.add(rInfo.activityInfo.applicationInfo
     .loadLabel(pm).toString());
   Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
     .loadLabel(pm).toString());
  } 
  lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }
}
查看更多
登录 后发表回答