Open top menu


How to Get All Installed App In ListView?

In this tutorial we will explain  how to get all install app from your android device.
explain step by step.....


activity_main.xml.
............................................................................................................
<?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="@android:id/list"        
android:layout_width="fill_parent"        
android:layout_height="wrap_content" />

</LinearLayout>
............................................................................................................

list_row.xml.
............................................................................................................
<?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="wrap_content" >

    
<ImageView        
android:id="@+id/app_icon"        
android:layout_width="50dp"        
android:layout_height="50dp"        
android:padding="3dp"        
android:scaleType="centerCrop" />

    
<LinearLayout        
android:layout_width="fill_parent"        
android:layout_height="fill_parent"        
android:gravity="center_vertical"        
android:orientation="vertical"        
android:paddingLeft="5dp" >

        
<TextView            
android:id="@+id/app_name"            
android:layout_width="fill_parent"            
android:layout_height="wrap_content"            
android:gravity="center_vertical"            
android:textStyle="bold" />

        
<TextView            
android:id="@+id/app_paackage"            
android:layout_width="fill_parent"           
 android:layout_height="wrap_content"            
android:gravity="center_vertical" />
    
</LinearLayout>

</LinearLayout>

............................................................................................................

AppListAdapter.java
............................................................................................................
public class AppListAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;

public AppListAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
        }

@Overridepublic int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
        }

@Overridepublic ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
        }

@Overridepublic long getItemId(int position) {
        return position;
        }

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context        
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_row, null);
        }

        ApplicationInfo applicationInfo = appsList.get(position);
        if (null != applicationInfo) {
        TextView appName = (TextView) view.findViewById(R.id.app_name);
        TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

        appName.setText(applicationInfo.loadLabel(packageManager));
        packageName.setText(applicationInfo.packageName);
        iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
        }
        return view;
        }
        }

............................................................................................................

MainActivity.java.
............................................................................................................
public class MainActivity  extends ListActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private AppListAdapter listadaptor = null;

    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        packageManager = getPackageManager();

        new LoadApplications().execute();
    }

   public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result = true;

        switch (item.getItemId()) {
            case R.id.menu_about: {
                displayAboutDialog();

                break;
            }
            default: {
                result = super.onOptionsItemSelected(item);

                break;
            }
        }

        return result;
    }

    private void displayAboutDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.about_title));
        builder.setMessage(getString(R.string.about_desc));

        builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("http://stacktips.com"));
                startActivity(browserIntent);
                dialog.cancel();
            }
        });
        builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        builder.show();
    }

    @Override    protected void onListItemClick(ListView l, View v, int position,
 long id) {
        super.onListItemClick(l, v, position, id);

        ApplicationInfo app = applist.get(position);
        try {
            
Intent intent = packageManager                    
.getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return applist;
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager
.getInstalledApplications(PackageManager.GET_META_DATA));
            listadaptor = new AppListAdapter(MainActivity.this,
                    R.layout.list_row, applist);

            return null;
        }

        @Override        protected void onCancelled() {
            super.onCancelled();
        }

        @Override        protected void onPostExecute(Void result) {
            setListAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override        protected void onPreExecute() {
            progress = ProgressDialog.show(MainActivity.this, null,
                    "Loading application info...");
            super.onPreExecute();
        }

        @Override        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}
............................................................................................................





Tagged

0 comments