When developing Android apps, sometimes we need to get information about all the apps installed on the device. This can not only be used to show the user's installation list, but also to check whether a specific application has been installed. This article will explain how to obtain this information programmatically.
1. Get basic information about installed applications
To get a list of applications installed on the device, we mainly use the PackageManager class. PackageManager provides methods to access system-level package information, including all installed packages.
1.1 Add permissions
First, add the necessary permissions to the file. Although it does not require special permissions to obtain the list of installed applications, for security reasons, ensure that the application has the ability to access the network (if you need to load icons or other resources from the network):
<uses-permission android:name="" />
1.2 Writing code
Next, write code in Activity or Fragment to get information about installed apps:
import ; import ; import ; import ; import ; import ; public class InstalledAppsActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_installed_apps); // Get the package manager PackageManager packageManager = getPackageManager(); // Get all installed applications List<ApplicationInfo> installedApplications = (PackageManager.GET_META_DATA); for (ApplicationInfo app : installedApplications) { String appName = (String) (app); // Application name String packageName = ; // Package name int versionCode = ; // Version number String versionName = ; // Version name // Print application information ("App Name: " + appName); ("Package Name: " + packageName); ("Version Code: " + versionCode); ("Version Name: " + versionName); } } }
In the above code, we obtain the list of all installed applications through the getPackageManager().getInstalledApplications() method, and iterate through each application to extract its name, package name, version number, version name and other information.
2. Filter non-user-installed applications
By default, getInstalledApplications() returns all applications, including those preinstalled on the system. If you only want to get the user installed applications themselves, you can filter by checking the ApplicationInfo.FLAG_SYSTEM flag:
List<ApplicationInfo> installedApplications = (PackageManager.GET_META_DATA); for (ApplicationInfo app : installedApplications) { if (( & ApplicationInfo.FLAG_SYSTEM) == 0) { // Non-system applications // Handle user-installed applications } }
3. Load the application icon
In addition to basic information, you may also need to display the app's icon. This can be achieved through the getApplicationIcon() method:
Drawable appIcon = (app); // Set icon in ImageView(appIcon);
Through the PackageManager class, we can easily get relevant information about all applications installed on our Android device. This is very useful for building application markets, management tools and other types of applications. Hope this article helps you implement similar functions in your own project.
Method supplement
In Android development, it is a common requirement to obtain all the applications installed on the device, such as creating an application manager, security software, or application marketplace. Here is a simple sample code showing how to get a list of installed apps in an Android app and display their names and icons.
Step 1: Add permissions
First, make sure your file contains permissions to query all installed applications:
<uses-permission android:name=".QUERY_ALL_PACKAGES"/>
Note: Starting with Android 11 (API level 30), if your app needs to access information from other apps, you need to declare the QUERY_ALL_PACKAGES permission. However, this permission may be restricted, especially for apps published to Google Play. So consider whether access to all the data of the application is really needed, or whether there are more specific needs.
Step 2: Write Java code
Next, write code in your Activity or Fragment to get and display this information:
import ; import ; import ; import ; import ; import ; import ; import ; import ; public class AppListActivity extends AppCompatActivity { private ListView listView; private ArrayAdapter<String> adapter; private List<String> appNames = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_app_list); listView = findViewById(); adapter = new ArrayAdapter<>(this, .simple_list_item_1, appNames); (adapter); loadInstalledApps(); } private void loadInstalledApps() { PackageManager pm = getPackageManager(); List<ApplicationInfo> packages = (PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { String appName = (String) (packageInfo); (appName + " - " + ); } // Update ListView (); } }
Step 3: Create a layout file
Create a layout file activity_app_list.xml to display the application list:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <ListView android: android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
explain
Permissions: The QUERY_ALL_PACKAGES permission allows the application to query all installed application package information.
Get the list of apps: Get the list of all installed apps via the getInstalledApplications method of PackageManager.
Display data: Use ArrayAdapter to bind the application name and package name to the ListView for display.
Things to note
Performance considerations: Obtaining large amounts of application information can affect performance, especially when there are a large number of applications on the device. You can consider batch loading or use asynchronous loading methods.
Permission Check: For devices running on Android 6.0 and above, it is recommended to request permissions at runtime to ensure that the application works properly.
The above is the basic method to obtain and display all the apps installed on your Android device. Hope this helps you! In Android development, obtaining a list of installed applications on your phone is a common requirement, such as for application management, security checking and other scenarios. Here is a detailed description of how to get a list of installed applications through Android code.
Basic steps to get the list of installed applications
Get PackageManager object: PackageManager is a class provided by the Android system to access the system's package information.
Use PackageManager to get information about installed applications: by calling the getInstalledPackages method, you can get all installed application package information.
Resolve application information: Extract the required information from the returned PackageInfo object, such as application name, package name, icon, etc.
Sample code
Here is a simple sample code showing how to get and display all installed app names and icons on your phone:
import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private ListView listView; private List<String> appNames = new ArrayList<>(); private List<Drawable> appIcons = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); listView = findViewById(); // Get PackageManager instance PackageManager packageManager = getPackageManager(); // Get all installed applications List<ApplicationInfo> installedApplications = (PackageManager.GET_META_DATA); for (ApplicationInfo applicationInfo : installedApplications) { // Get the application name String appName = (applicationInfo).toString(); // Get the application icon Drawable appIcon = (applicationInfo); (appName); (appIcon); } // Set the ListView adapter ArrayAdapter<String> adapter = new ArrayAdapter<>(this, .simple_list_item_1, appNames); (adapter); } }
Detailed description
Get PackageManager object:
PackageManager packageManager = getPackageManager();
Get a list of installed applications:
List<ApplicationInfo> installedApplications = (PackageManager.GET_META_DATA);
Here, the getInstalledApplications method is used and the GET_META_DATA flag is passed in to get more application information.
Traversing the installed applications:
for (ApplicationInfo applicationInfo : installedApplications) { String appName = (applicationInfo).toString(); Drawable appIcon = (applicationInfo); (appName); (appIcon); }
The getApplicationLabel method is used to get the name of the application.
The getApplicationIcon method is used to get the application's icon.
Setting up ListView Adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, .simple_list_item_1, appNames); (adapter);
Use ArrayAdapter to bind the list of app names to the ListView.
Things to note
Permissions: Getting the list of installed applications usually does not require additional permissions, but if it involves reading the application's detailed information (such as permissions, signatures, etc.), it may require corresponding permissions.
Performance: If the application list is very large, it is recommended to use asynchronous loading or paging loading to optimize performance.
With the above steps and sample code, you can easily get and display a list of installed applications in your Android app.
When developing Android apps, sometimes we need to get information about all the apps installed on the device.