For getting the list of all apps installed in the device and to show it on a listview we can use the following code:
MainActivity.java
activity_main.xml
MainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
 ArrayList results_sys_app = new ArrayList();
 ArrayAdapter<applicationinfo>adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  final List<applicationinfo> pkgAppsList = this.getPackageManager().getInstalledApplications(    PackageManager.GET_UNINSTALLED_PACKAGES);
  PackageManager pm2 = (PackageManager) this.getPackageManager();
  ListView L1 = (ListView) findViewById(R.id.listView1);
  for (int i = 0; i < pkgAppsList.size(); i++) {
   /*
    * for getting the app name
    */
   results_sys_app.add(pkgAppsList.get(i).loadLabel(pm2).toString());
   /*
    * for getting the icon of the app we can use the following
    * commented code
    */
   
   // results_sys_app.add(pkgAppsList.get(i).loadIcon(pm2).toString());
   adapter = new ArrayAdapter<applicationinfo>(this,
     android.R.layout.simple_list_item_1, android.R.id.text1,
     results_sys_app);
   L1.setAdapter(adapter);
  }
 }
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.allapps.MainActivity"
    tools:ignore="MergeRootFrame" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</FrameLayout>
Comments
Post a Comment