Saturday, 14 May 2016

How to get list of installed apps in android?

Get list of user installed apps in android

In this tutorial we will learn how to show list of installed application in your android phone.

Here is the code snippet for the same.You can download the source code from github

package com.roy.installedapps;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.recycler_view)
RecyclerView recyclerView;
private ArrayList<AppModel> mList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
getApplicationIstalled();
ApplIstAdapter adapter = new ApplIstAdapter(this,mList);
recyclerView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getApplicationIstalled() {
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
ApplicationInfo app = null;
try {
if ( (packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
AppModel model = new AppModel();
try {
Drawable icon = getPackageManager().getApplicationIcon(packageInfo.processName);
model.setDrawable(icon);
} catch (Exception e) {
}
String AppName =packageInfo.loadLabel(getPackageManager()).toString();
model.setPackageName(packageInfo.packageName);
model.setAppName(AppName);
mList.add(model);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment