0

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

  • Stop confusing others. First of all, post your code and also the errors and describe your problem. Only then one can help you. No one could guess the issue you encounter without looking at your code. – Sahil Mahajan Mj Jan 14 '13 at 11:41
  • Ok well for the codes supplied by Nipun Gogia, I encountered errors in the "for (ResolveInfo rInfo : list) {" part where the list is underlined in red. Also, for the "lView = (ListView) findViewById(R.id.list1);" part, the list1 is also underlined in red. The 2 variables "private ListView lView;" and "private ArrayList results = new ArrayList();" had their respective variable name (IView and results) underlined in red too. any idea why they are in red? – user1977094 Jan 14 '13 at 11:48
  • they are read, because they are not just initialised. – Sahil Mahajan Mj Jan 14 '13 at 11:50

2 Answers2

0
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));
    }
}
Nipun Gogia
  • 1,804
  • 1
  • 10
  • 16
  • answer might be correct, but unfortunately, I encountered a lot of errors as I do not know what to change in order to achieve my objective which has left me clueless despite having googled and searched for the answers on this site itself. would appreciate if more help were provided like how does the above code get together with the xml file – user1977094 Jan 14 '13 at 11:39
  • Ok well for the codes supplied by Nipun Gogia, I encountered errors in the "for (ResolveInfo rInfo : list) {" part where the list is underlined in red. Also, for the "lView = (ListView) findViewById(R.id.list1);" part, the list1 is also underlined in red. The 2 variables "private ListView lView;" and "private ArrayList results = new ArrayList();" had their respective variable name (IView and results) underlined in red too. any idea why they are in red? – user1977094 Jan 14 '13 at 11:49
  • have you made a xml containing list – Nipun Gogia Jan 14 '13 at 11:51
  • just replace this line "List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);" – Nipun Gogia Jan 14 '13 at 11:54
0

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.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 12,525
  • 8
  • 53
  • 98
  • Thanks it worked for the retrieving of app just that it is in text list form instead of the picture icon form and that the apps could not be accessed. Would any slight change in the code be able to morph that to icon and make the apps accessible? – user1977094 Jan 14 '13 at 12:35
  • Do you want to open the app on clicking on it. That is totally a different thing. You have to pass the intent to that specific application to open it. – Sahil Mahajan Mj Jan 14 '13 at 12:38
  • So in that case how would the code be like? Sorry for imposing too much but I am really bad at Android programming as I have not done much of it and am trying to meet tight deadlines – user1977094 Jan 14 '13 at 12:40
  • @user1977094 : See my edited answer for your next issue. It will definitely solve your issue. and also dont forget to mark it as an answer, if it helped. – Sahil Mahajan Mj Jan 14 '13 at 12:51
  • The above code doesn't seems to achieve the result that I want, whether it is the list becoming icon, or the app being accessible, in fact, if I did not see wrongly, there isn't any changes in the code, but I may be wrong. – user1977094 Jan 14 '13 at 13:20
  • I haven't changed the code, just see the **Edit-** part. If you want to show the icon of the application along with its name, then you have to create a customized list iwth imageview and textview – Sahil Mahajan Mj Jan 14 '13 at 13:25