读取手机上所有应用程序并显示(APP)
pd = ProgressDialog.show(getActivity(), "请稍候..", "正在收集软件信息...", true,false); Thread thread = new Thread(this); thread.start();
@Override public void run() { if(mlistAppInfo==null){ mlistAppInfo = new ArrayList<AppInfo>(); queryAppInfo(); } handler.sendEmptyMessage(0); } private Handler handler = new Handler() { public void handleMessage(Message msg) { if(mlistAppInfo!=null&&mlistAppInfo.size()==1) { Intent intent = mlistAppInfo.get(0).getIntent(); startActivity(intent); } else if(mlistAppInfo!=null&&mlistAppInfo.size()>1){ View appView=LayoutInflater.from(getActivity()).inflate(R.layout.yue_list_new, null); listview = (ListView) appView.findViewById(R.id.listview); BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter( getActivity(), mlistAppInfo); listview.setAdapter(browseAppAdapter); listview.setOnItemClickListener(LvxingbaoFragment.this); new AlertDialog.Builder(getActivity()).setView(appView).show(); } pd.dismiss(); } }; public void queryAppInfo() { PackageManager pm = getActivity().getPackageManager(); // 获得PackageManager对象 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); // 通过查询,获得所有ResolveInfo对象. List<ResolveInfo> resolveInfos = pm .queryIntentActivities(mainIntent, 0); // 调用系统排序 , 根据name排序 // 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序 Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm)); if (mlistAppInfo != null) { mlistAppInfo.clear(); for (ResolveInfo reInfo : resolveInfos) { String appLabel = (String) reInfo.loadLabel(pm); String activityName = reInfo.activityInfo.name; String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名 // String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标 // 为应用程序的启动Activity 准备Intent Intent launchIntent = new Intent(); launchIntent.setComponent(new ComponentName(pkgName, activityName)); // 创建一个AppInfo对象,并赋值 AppInfo appInfo = new AppInfo(); appInfo.setAppLabel(appLabel); appInfo.setPkgName(pkgName); appInfo.setAppIcon(icon); appInfo.setIntent(launchIntent); mlistAppInfo.add(appInfo); // 添加至列表中 // String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name // System.out.println(appLabel + " activityName---" + activityName // + " pkgName---" + pkgName); } } } class BrowseApplicationInfoAdapter extends BaseAdapter { private List<AppInfo> mlistAppInfo = null; LayoutInflater infater = null; public BrowseApplicationInfoAdapter(Context context, List<AppInfo> apps) { infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mlistAppInfo = apps ; } @Override public int getCount() { // TODO Auto-generated method stub System.out.println("size" + mlistAppInfo.size()); return mlistAppInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mlistAppInfo.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertview, ViewGroup arg2) { System.out.println("getView at " + position); View view = null; ViewHolder holder = null; if (convertview == null || convertview.getTag() == null) { view = infater.inflate(R.layout.item_add_score, null); holder = new ViewHolder(view); view.setTag(holder); } else{ view = convertview ; holder = (ViewHolder) convertview.getTag() ; } AppInfo appInfo = (AppInfo) getItem(position); holder.appIcon.setImageDrawable(appInfo.getAppIcon()); holder.tvAppLabel.setText(appInfo.getAppLabel()); //holder.tvPkgName.setText(appInfo.getPkgName()); return view; } } class ViewHolder { ImageView appIcon; TextView tvAppLabel; //TextView tvPkgName; public ViewHolder(View view) { this.appIcon = (ImageView) view.findViewById(R.id.add_score_app_img); this.tvAppLabel = (TextView) view.findViewById(R.id.add_score_app_name); // this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName); } } class AppInfo { private String appLabel; //应用程序标签 private Drawable appIcon ; //应用程序图像 private Intent intent ; //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity private String pkgName ; //应用程序所对应的包名 public AppInfo(){} public String getAppLabel() { return appLabel; } public void setAppLabel(String appName) { this.appLabel = appName; } public Drawable getAppIcon() { return appIcon; } public void setAppIcon(Drawable appIcon) { this.appIcon = appIcon; } public Intent getIntent() { return intent; } public void setIntent(Intent intent) { this.intent = intent; } public String getPkgName(){ return pkgName ; } public void setPkgName(String pkgName){ this.pkgName=pkgName ; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。