在我的应用程序中,我必须在列表视图中列出已安装的应用程序。应用程序在后台线程中获取已安装的应用程序。要求是即使未完成获取也希望填充列表。也就是说,如果 arraylist 中有一个条目,则显示带有该项目的 arraylist。这可能吗?请帮助我。在此先感谢
问问题
101 次
3 回答
2
首先,这只有在您使用循环在后台处理应用程序数据时才有可能。一篇大纲:
保存 App 信息的类:
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();
于 2012-11-14T08:27:52.563 回答
1
您可以动态地向/从适配器添加/删除项目ListView
,所以是的,一旦有新项目可用,您就可以从后台线程中一个一个地填充项目列表。
于 2012-11-14T07:57:56.667 回答
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);
}
}
如果您对此代码有任何问题,请告诉我。
于 2012-11-14T07:49:58.153 回答