android studio 如何将已安装应用的列表打印到 listView 中
android studio how to print list of installed apps into a listView
我在 Internet 上找到了一些代码来获取当前安装的应用程序的详细信息,但我不确定如何将其实现到列表中。我已经在 xml 文件中创建了列表,但我不太确定如何将数据实现到 listView 中。获取app数据的代码为-
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
for (Object object : pkgAppsList)
{
ResolveInfo info = (ResolveInfo) object;
Drawable icon = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
String strAppName = info.activityInfo.applicationInfo.publicSourceDir.toString();
String strPackageName = info.activityInfo.applicationInfo.packageName.toString();
final String title = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
}
您可以先创建一个字符串列表,如下所示:
final ArrayList<String> list = new ArrayList<String>();
然后在您的代码中,通过 list.add(strAppName);
将 strAppName 添加到列表中
并制作一个数组适配器并将其设置为您的列表视图:
final ListView listview = (ListView) findViewById(R.id.listview);
final ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
我在 Internet 上找到了一些代码来获取当前安装的应用程序的详细信息,但我不确定如何将其实现到列表中。我已经在 xml 文件中创建了列表,但我不太确定如何将数据实现到 listView 中。获取app数据的代码为-
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
for (Object object : pkgAppsList)
{
ResolveInfo info = (ResolveInfo) object;
Drawable icon = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
String strAppName = info.activityInfo.applicationInfo.publicSourceDir.toString();
String strPackageName = info.activityInfo.applicationInfo.packageName.toString();
final String title = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
}
您可以先创建一个字符串列表,如下所示:
final ArrayList<String> list = new ArrayList<String>();
然后在您的代码中,通过 list.add(strAppName);
并制作一个数组适配器并将其设置为您的列表视图:
final ListView listview = (ListView) findViewById(R.id.listview);
final ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);