配置文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lvProcessbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
程序:
package com.amaker.app;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AA extends Activity {
public static final int MSG_UPDATE_LIST = 18;
private ListView mApps;
private Context mContext;
private List<String> mAppList;
private ProgressDialog dialog;
private ArrayAdapter mAdapter;
private boolean mIsLoaded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ListView 列表 mApps
mApps = (ListView)findViewById(R.id.lvProcessbar);
mContext = this;
mAppList = new ArrayList<String>();
mAdapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, mAppList);
mApps.setAdapter(mAdapter);
// 设置正在处理窗口
dialog = new ProgressDialog(mContext);
dialog.setIcon(R.drawable.icon);
dialog.setTitle("ProgressDialog");
dialog.setMessage("Please wait while loading application list...");
dialog.setCancelable(false);
dialog.show();
// 开始动态加载线程
mThreadLoadApps.start();
mApps.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
mAppList.remove(position);
mAdapter.notifyDataSetChanged();
}
});
// 获取已经安装程序列表
PackageManager pm = mContext.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent,0);
// 逐项添加程序,并发送消息更新ListView列表。
for (int i=0; i<list.size(); i++) {
mAppList.add(list.get(i).loadLabel(pm).toString());
System.out.println("-------------------------->"+(list.get(i).loadLabel(pm).toString()));
mAdapter.notifyDataSetChanged();
}
mIsLoaded = true;
}
private Thread mThreadLoadApps = new Thread(){
@Override
public void run() {
int i = 0;
while (!mIsLoaded) {
try {
sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
// 关闭正在处理窗口
dialog.dismiss();
}
};
}