The PackageManager class is used to get the information of application packages that are currently installed on android device. You can get an instance of PackageManager class using getPackageManager().
Below example will show you how to implement this.
1. XML layouts for the application.
activity_installed_app.xml
This layout will be used to show the list of all installed application in a device.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view"/>
</LinearLayout>
list_row.xml
This layout will be used by adapter class to display application information like application icon, application package name and application name in ListView.
<?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_package"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
2. Application Java classes
InstalledAppActivity.java
This is the main activity class which is used to initialize and get the list of installed application using PackageManager class. This activity class is also using custom adapter "MyAdapter.java" class for ListView.
package com.testffmpeg.activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.testffmpeg.R;
import com.testffmpeg.adapter.MyAdapter;
import java.util.List;
public class InstalledAppActivity extends AppCompatActivity {
private ListView listview;
private MyAdapter myAdapter = null;
private PackageManager packageManager = null;
private List<ApplicationInfo> applicationList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_installed_app);
listview = (ListView) findViewById(R.id.list_view);
packageManager = getPackageManager();
//get a list of installed apps.
applicationList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
if(applicationList != null) {
myAdapter = new MyAdapter(InstalledAppActivity.this, applicationList);
listview.setAdapter(myAdapter);
}
}
}
MyAdapter.java
package com.testffmpeg.adapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
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 com.testffmpeg.R;
import java.util.List;
public class MyAdapter extends BaseAdapter {
List<ApplicationInfo> appsList;
Context mContext;
public MyAdapter(Context context,List<ApplicationInfo> appList){
this.appsList = appList;
this.mContext = context;
}
@Override
public int getCount() {
return appsList.size();
}
@Override
public Object getItem(int position) {
return appsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(null == view) {
view = LayoutInflater.from(mContext).inflate(R.layout.list_row, null);
}
ApplicationInfo applicationInfo = appsList.get(position);
if (null != applicationInfo) {
TextView appName = (TextView) view.findViewById(R.id.app_name);
TextView appDescription = (TextView) view.findViewById(R.id.app_package);
ImageView appIcon = (ImageView) view.findViewById(R.id.app_icon);
appName.setText(applicationInfo.loadLabel(mContext.getPackageManager()));
appDescription.setText(applicationInfo.packageName);
appIcon.setImageDrawable(applicationInfo.loadIcon(mContext.getPackageManager()));
}
return view;
}
}
0 Comment(s)