Obtain information about installed apps on android devices.
This article describes how to obtain information about installed applications on a device, including the Application name, package name, and icon.
After obtaining the information list, you can select a record to start the corresponding application!
1. Code Implementation
Package com. example. showapplist; import java. util. arrayList; import java. util. list; import android. content. pm. applicationInfo; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. OS. bundle; import android. support. v4.app. listFragment; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. listView; public class AppListFragment extends ListFragment {private ArrayList appList = new ArrayList (); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getAppList (); AppAdapter adapter = new AppAdapter (this. getActivity (), appList); setListAdapter (adapter) ;}@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return super. onCreateView (inflater, container, savedInstanceState) ;}@ Overridepublic void onListItemClick (ListView l, View v, int position, long id) {// start the selected application startActivity (appList. get (position ). appIntent);}/*** obtain the list of non-system application information */private void getAppList () {PackageManager pm = this. getActivity (). getPackageManager (); // Return a List of all packages that are installed on the device. list
Packages = pm. getInstalledPackages (0); for (PackageInfo packageInfo: packages) {// determine the system/non-system application if (packageInfo. applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM) = 0) // non-system application {AppInfo info = new AppInfo (); info. appName = packageInfo. applicationInfo. loadLabel (pm ). toString (); info. pkgName = packageInfo. packageName; info. appIcon = packageInfo. applicationInfo. loadIcon (pm); // gets the Intent of the application installation package, which is used to start the application info. appIntent = pm. getLaunchIntentForPackage (packageInfo. packageName); appList. add (info) ;}else {// System Application }}}}
The getAppList () method obtains the application information installed on the current device.
Use the following code to determine whether an application is a system application or a non-system application:
// Determine the system/non-system application if (packageInfo. applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM) = 0) // non-system application {} else {// System Application}
Use the following code to obtain the Intent required to start another application:
// Obtain the Intent of the application installation package to start the application info. appIntent = pm. getLaunchIntentForPackage (packageInfo. packageName );