获取 Android 手机中安装的所有应用程序列表的不同方法
在本文中,我们将显示您的 Android 手机上所有已安装应用程序的列表。所以在这里我们将学习如何以三种不同的方式实现该功能。请注意,我们将使用Java语言来实现这个项目。
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。
步骤 2:使用 activity_main.xml 文件
导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。
XML
Java
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) {
// get list of all the apps installed
List infos = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
// create a list with sze of total number of apps
String[] apps = new String[infos.size()];
int i = 0;
// add all the app name in string list
for (ApplicationInfo info : infos) {
apps[i] = info.packageName;
i++;
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(infos.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
Java
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) {
// get list of all the apps installed
List packList = getPackageManager().getInstalledPackages(0);
String[] apps = new String[packList.size()];
for (int i = 0; i < packList.size(); i++) {
PackageInfo packInfo = packList.get(i);
apps[i] = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(packList.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
Java
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) throws PackageManager.NameNotFoundException {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// get list of all the apps installed
List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
List componentList = new ArrayList();
String name = null;
int i = 0;
// get size of ril and create a list
String[] apps = new String[ril.size()];
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
// get package
Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
// if activity label res is found
if (ri.activityInfo.labelRes != 0) {
name = res.getString(ri.activityInfo.labelRes);
} else {
name = ri.activityInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
}
apps[i] = name;
i++;
}
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(ril.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
方法一
转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) {
// get list of all the apps installed
List infos = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
// create a list with sze of total number of apps
String[] apps = new String[infos.size()];
int i = 0;
// add all the app name in string list
for (ApplicationInfo info : infos) {
apps[i] = info.packageName;
i++;
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(infos.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
输出:
方法二
转到主活动。 Java文件,参考如下代码。下面是 MainActivity 的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) {
// get list of all the apps installed
List packList = getPackageManager().getInstalledPackages(0);
String[] apps = new String[packList.size()];
for (int i = 0; i < packList.size(); i++) {
PackageInfo packInfo = packList.get(i);
apps[i] = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(packList.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
输出:
方法三
转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise layout
listView = findViewById(R.id.listview);
text = findViewById(R.id.totalapp);
}
public void getallapps(View view) throws PackageManager.NameNotFoundException {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// get list of all the apps installed
List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
List componentList = new ArrayList();
String name = null;
int i = 0;
// get size of ril and create a list
String[] apps = new String[ril.size()];
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
// get package
Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
// if activity label res is found
if (ri.activityInfo.labelRes != 0) {
name = res.getString(ri.activityInfo.labelRes);
} else {
name = ri.activityInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
}
apps[i] = name;
i++;
}
}
// set all the apps name in list view
listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, apps));
// write total count of apps available.
text.setText(ril.size() + " Apps are installed");
}
@Override
protected void onStart() {
super.onStart();
}
}
输出: