在我的应用程序中,我必须在列表视图中列出已安装的应用程序。应用程序在后台线程中提取已安装的应用程序。要求是想要在未完成提取时填充列表,即如果在arraylist中有一个条目,则显示带有该项目的arraylist。这可能吗?请帮帮我。谢谢你提前
答案 0 :(得分:2)
首先,只有在后台使用循环处理应用程序数据时才可以这样做。大纲:
持有应用信息的类:
Class AppData {
public final Drawable icon;
public final String name;
public AppData(Drawable i, String n){
this.icon = i;
this.name = n;
}
}
AsynckTask搜索应用程序:
AsyncTask<Void,AppData,Void> scanAppsTask = new AsyncTask<Void,AppData,Void>{
@Override
public Void doInBackground(Void... args){
//--get list---
List<ApplicationInfo> apps = mPm.getInstalledApplications(
PackageManager.GET_UNINSTALLED_PACKAGES |
PackageManager.GET_DISABLED_COMPONENTS);
//--run a loop--
for(ApplicationInfo appInfo : apps){
AppData newFound;
//---find app details, load app icon etc---
publishProgress(newFound);
}
//---done---
return null;
}
@Override
public void onProgressUpdate(AppData... data){
//---update list for every app found----
myListAdapter.add(data[0]);
myListAdapter.notifyDataSetChanged();
}
}
scanAppsTask.execute();
答案 1 :(得分:1)
我正在开发一个类似的应用程序,我需要获取所有已安装的应用程序,甚至是内置应用程序。我用以下代码完成了这个,
TextView data;
ImageView image1;
LinearLayout holdlayout;
View l1;
private ArrayList results = new ArrayList();
List<ResolveInfo> list;
TextView result;
String str="";
Drawable icon;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1 = findViewById(R.id.Layout1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
str = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString() + "\n";
results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
icon = rInfo.activityInfo.applicationInfo.loadIcon(pm);
holdlayout = new LinearLayout(getApplicationContext());
holdlayout.setOrientation(LinearLayout.HORIZONTAL);
data = new TextView(getApplicationContext());
data.setText(str);
image1 = new ImageView(getApplicationContext());
image1.setBackgroundDrawable(icon);
((ViewGroup) holdlayout).addView(image1);
((ViewGroup) holdlayout).addView(data);
((ViewGroup) l1).addView(holdlayout);
}
}
如果您在此代码中遇到任何问题,请与我们联系。
答案 2 :(得分:1)
您可以动态地向/从ListView
的适配器添加/删除项目,因此,只要新项目可用,您就可以从后台线程逐个填充项目列表。