public class AppListView extends LinearLayout implements OnItemClickListener{
private final static String TAG = "AppListView";
private ListView mListView;
private TextView mTvTitle;
private List<AppInfo> mAppList;
private Context mContext;
private LayoutInflater mInflater;
private PackageManager mPacManager;
public AppListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public AppListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public AppListView(Context context)
{
super(context);
init(context);
}
private void init(Context c)
{
mContext = c;
mInflater = (LayoutInflater)c.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
this.addView(mInflater.inflate(R.layout.activity_main,null,false));
mListView = (ListView)this.findViewById(R.id.listView);
mTvTitle = (TextView)this.findViewById(R.id.title);
loadAppData();
mListView.setAdapter(new MyAdapter(c));
mListView.setOnItemClickListener(this);
Log.d(TAG, "一共"+mAppList.size());
}
// 加载应用软件数据 软件名称,包名,对应的图标等等
private void loadAppData()
{
if(mAppList != null){
mAppList.clear();
}else{
mAppList = new ArrayList<AppInfo>();
}
mPacManager = mContext.getPackageManager();
List<PackageInfo> packages = mPacManager.getInstalledPackages(0);
for(int i=0; i<packages.size(); i++){
PackageInfo pi = packages.get(i);
AppInfo ai = new AppInfo();
ai.packageName = pi.packageName;
ai.appName = pi.applicationInfo.loadLabel(mPacManager).toString();
ai.appIcon = pi.applicationInfo.loadIcon(mPacManager);
mAppList.add(ai);
}
mTvTitle.setText("本机所安装的软件总数:"+packages.size()+"个");
}
// 为ListView自定义适配器
class MyAdapter extends BaseAdapter
{
public MyAdapter(Context c)
{
mContext = c;
}
@Override
public int getCount() {
return mAppList == null?0:mAppList.size();
}
@Override
public Object getItem(int arg0) {
return mAppList == null?null:mAppList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View view;
if(arg1 == null){
view = mInflater.inflate(R.layout.app_list_item, null);
}else{
view = arg1;
}
AppInfo ai = (AppInfo)getItem(arg0);
ImageView appIcon = (ImageView)view.findViewById(R.id.appIcon);
TextView appName = (TextView)view.findViewById(R.id.appName);
TextView appPackage = (TextView)view.findViewById(R.id.appPackage);
appIcon.setImageDrawable(ai.appIcon);
appName.setText(ai.appName);
appPackage.setText(ai.packageName);
return view;
}
}
// 处理ListView的item的点击操作,我这里是启动该应用程序
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(mContext, arg2+"", Toast.LENGTH_SHORT).show();
// 获取本次item的包名
String packName = mAppList.get(arg2).packageName;
// 启动此程序
Intent intent = mPacManager.getLaunchIntentForPackage(packName);
mContext.startActivity(intent);
}
// 软件载体
public class AppInfo {
// 软件名称
public String appName="";
// 软件包名
public String packageName="";
// 软件图标
public Drawable appIcon=null;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有