Android get the list of all installed apps with icons
We can get the list of all installed apps by Using PackageManager class. We can get the application name, package name, version no, icon etc using this class. In below example i have checked ApplicationInfo.FLAG_SYSTEM=0 which will exclude system apps. I you want to keep system app also in your list then disable this check.
Steps to get installed application in Android
1. Create PackageInformation class.
2. Create Custom Adapter to handle list.
3. Design XML layout for list.
4. Access PackageInformation from MainActivity.
1. Create PackageInformation class.
This class has method getInstalledApps, this method is using PackageManager to get all the installed apps excluding system apps.
PackageInformation.java
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import java.util.ArrayList;
import java.util.List;
public class PackageInformation {
private Context mContext;
public PackageInformation(Context context) {
mContext = context;
}
public ArrayList<InfoObject> getInstalledApps() {
ArrayList<InfoObject> listObj = new ArrayList<InfoObject>();
final PackageManager packageManager = mContext.getPackageManager();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
InfoObject newInfo = new InfoObject();
newInfo.appname = applicationInfo.loadLabel(packageManager).toString();
newInfo.packagename = applicationInfo.packageName;
newInfo.icon = applicationInfo.loadIcon(packageManager);
newInfo.launchactivity = packageManager.getLaunchIntentForPackage(applicationInfo.packageName);
listObj.add(newInfo);
}
}
return listObj;
}
class InfoObject {
public String appname = "";
public String packagename = "";
public Intent launchactivity;
public Drawable icon;
}
}
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import java.util.ArrayList;
import java.util.List;
public class PackageInformation {
private Context mContext;
public PackageInformation(Context context) {
mContext = context;
}
public ArrayList<InfoObject> getInstalledApps() {
ArrayList<InfoObject> listObj = new ArrayList<InfoObject>();
final PackageManager packageManager = mContext.getPackageManager();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
InfoObject newInfo = new InfoObject();
newInfo.appname = applicationInfo.loadLabel(packageManager).toString();
newInfo.packagename = applicationInfo.packageName;
newInfo.icon = applicationInfo.loadIcon(packageManager);
newInfo.launchactivity = packageManager.getLaunchIntentForPackage(applicationInfo.packageName);
listObj.add(newInfo);
}
}
return listObj;
}
class InfoObject {
public String appname = "";
public String packagename = "";
public Intent launchactivity;
public Drawable icon;
}
}
2. Create Custom Adapter to handle list.
CustomAdapterAppList.java
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapterAppList extends BaseAdapter {
private ArrayList<PackageInformation.InfoObject> mObjectApps;
private Context mContext;
public CustomAdapterAppList(Context context, ArrayList<PackageInformation.InfoObject> apps) {
this.mContext = context;
this.mObjectApps = apps;
}
@Override
public int getCount() {
return mObjectApps.size();
}
@Override
public Object getItem(int position) {
return mObjectApps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.layout_adapter_custom_app_list, parent, false);
holder = new ViewHolder();
holder.appName = (TextView) convertView.findViewById(R.id.text_app_name);
holder.appIcon = (ImageView) convertView.findViewById(R.id.image_app_icon);
holder.appOption = (ImageView) convertView.findViewById(R.id.image_option);
//Set values
final PackageInformation.InfoObject infoObject = mObjectApps.get(position);
holder.appName.setText(infoObject.appname);
holder.appIcon.setImageDrawable(infoObject.icon);
//Click action for app
holder.appOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Open the specific App Info page:
String packageName = infoObject.packagename;
try {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
//Open the generic Apps manager
Intent intent = new Intent(Settings.ACTION_SETTINGS);
mContext.startActivity(intent);
}
}
});
return convertView;
}
private class ViewHolder {
TextView appName;
TextView appVersion;
ImageView appIcon;
ImageView appOption;
}
}
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapterAppList extends BaseAdapter {
private ArrayList<PackageInformation.InfoObject> mObjectApps;
private Context mContext;
public CustomAdapterAppList(Context context, ArrayList<PackageInformation.InfoObject> apps) {
this.mContext = context;
this.mObjectApps = apps;
}
@Override
public int getCount() {
return mObjectApps.size();
}
@Override
public Object getItem(int position) {
return mObjectApps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.layout_adapter_custom_app_list, parent, false);
holder = new ViewHolder();
holder.appName = (TextView) convertView.findViewById(R.id.text_app_name);
holder.appIcon = (ImageView) convertView.findViewById(R.id.image_app_icon);
holder.appOption = (ImageView) convertView.findViewById(R.id.image_option);
//Set values
final PackageInformation.InfoObject infoObject = mObjectApps.get(position);
holder.appName.setText(infoObject.appname);
holder.appIcon.setImageDrawable(infoObject.icon);
//Click action for app
holder.appOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Open the specific App Info page:
String packageName = infoObject.packagename;
try {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
//Open the generic Apps manager
Intent intent = new Intent(Settings.ACTION_SETTINGS);
mContext.startActivity(intent);
}
}
});
return convertView;
}
private class ViewHolder {
TextView appName;
TextView appVersion;
ImageView appIcon;
ImageView appOption;
}
}
3. Design XML layout for list.
layout_adapter_custom_app_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- list row -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:padding="10dp">
<ImageView
android:id="@+id/image_app_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_weight=".5"
tools:ignore="ContentDescription"/>
<TextView
android:id="@+id/text_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginLeft="10dp"
android:text="App name" />
<ImageView
android:id="@+id/image_option"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_weight=".5"
tools:ignore="ContentDescription"/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- list row -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:padding="10dp">
<ImageView
android:id="@+id/image_app_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_weight=".5"
tools:ignore="ContentDescription"/>
<TextView
android:id="@+id/text_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginLeft="10dp"
android:text="App name" />
<ImageView
android:id="@+id/image_option"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_weight=".5"
tools:ignore="ContentDescription"/>
</LinearLayout>
</LinearLayout>
4. Access PackageInformation from MainActivity.
MainActivity.java
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listViewApps=(ListView)findViewById(R.id.list_app);
//Get the information of all the installed application in device
PackageInformation packageInformation=new PackageInformation(MainActivity.this);
ArrayList<PackageInformation.InfoObject> apps= packageInformation.getMovableApps();
CustomAdapterAppList adapterAppList=new CustomAdapterAppList(MainActivity.this,apps);
adapterAppList.notifyDataSetChanged();
listViewApps.setAdapter(adapterAppList);
}
}
Run the application and you can see the list of applications.
No comments:
Post a Comment