PackageManager获取应用列表

package com.pack;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;


public class PackageActivity extends Activity {
/** Called when the activity is first created. */
PackageManager pm;
List<PackageInfo> appInfo;
ArrayList<HashMap<String, Object>> appArray = null;
ListView lv = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) this.findViewById(R.id.listView);

pm = this.getPackageManager();

appInfo = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

                //getInstalledPackages(flags):flage的参数有多种

Iterator<PackageInfo> it = appInfo.iterator();
while (it.hasNext()) {
PackageInfo app = (PackageInfo) it.next();
// if ((app.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
HashMap map = new HashMap();
map.put("icon", app.applicationInfo.loadIcon(pm));
map.put("appName", app.applicationInfo.loadLabel(pm));
map.put("packageName", app.packageName);
if (appArray == null)
appArray = new ArrayList();
appArray.add(map);
//}
}
SimpleAdapter adapter = new SimpleAdapter(this, appArray,
R.layout.packitem, new String[] { "icon", "appName",
"packageName" }, new int[] { R.id.icon, R.id.appName,
R.id.packageName });
lv.setAdapter((ListAdapter) adapter);
        
adapter.setViewBinder(new ViewBinder(){
            public boolean setViewValue(View view,Object data,String textRepresentation){
                if(view instanceof ImageView && data instanceof Drawable){
                    ImageView iv=(ImageView)view;
                    iv.setImageDrawable((Drawable)data);
                    return true;
                }
                else if(view instanceof TextView && data instanceof String){
                 TextView tv = (TextView)view;
                 tv.setText((String)data);
                
                 return true;
                }
                else return false;
            }
        });
lv.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String packageName = (String) appArray.get(arg2).get(
"packageName");
Intent intent = pm.getLaunchIntentForPackage(packageName);
Log.i("PackageActivity", "open intent " + packageName);
if (intent != null) {
startActivity(intent);
} else {
Log.e("PackageActivity", "startActivity error2.");
}
}


});
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值