applist

package com.proj.applist;


import android.content.Intent;
import android.graphics.drawable.Drawable;


public class AppInfo {
private String appLabel;    
   private Drawable appIcon ;  
   private Intent intent ;     
   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 ;
   }

}


package com.proj.applist;


import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public 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.browse_app_item, 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.imgApp);
            this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
            this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
        }
    }


}

package com.proj.applist;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
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.ListView;


public class MainActivity extends Activity implements OnItemClickListener{


private ListView listview = null;


   private List<AppInfo> mlistAppInfo = null;


   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.broswe_app_list);


       listview = (ListView) findViewById(R.id.listviewApp);
       mlistAppInfo = new ArrayList<AppInfo>();
       queryAppInfo(); // 鏌ヨ鎵€鏈夊簲鐢ㄧ▼搴忎俊鎭?
       BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
               this, mlistAppInfo);
       listview.setAdapter(browseAppAdapter);
       listview.setOnItemClickListener(this);
   }
   // 鐐瑰嚮璺宠浆鑷宠搴旂敤绋嬪簭
   public void onItemClick(AdapterView<?> arg0, View view, int position,
           long arg3) {
       // TODO Auto-generated method stub
       Intent intent = mlistAppInfo.get(position).getIntent();
       startActivity(intent);
   }
   // 鑾峰緱鎵€鏈夊惎鍔ˋctivity鐨勪俊鎭紝绫讳技浜嶭aunch鐣岄潰
   public void queryAppInfo() {
       PackageManager pm = this.getPackageManager(); //鑾峰緱PackageManager瀵硅薄
       Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
       mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
       // 閫氳繃鏌ヨ锛岃幏寰楁墍鏈塕esolveInfo瀵硅薄.
       List<ResolveInfo> resolveInfos = pm
               .queryIntentActivities(mainIntent, 0);//0 Launcher涓殑搴旂敤,PackageManager.MATCH_DEFAULT_ONLY鎵€鏈夊簲鐢?


            Log.d("count","appList_count:"+resolveInfos.size());
       // 璋冪敤绯荤粺鎺掑簭 锛?鏍规嵁name鎺掑簭
       // 璇ユ帓搴忓緢閲嶈锛屽惁鍒欏彧鑳芥樉绀虹郴缁熷簲鐢紝鑰屼笉鑳藉垪鍑虹涓夋柟搴旂敤绋嬪簭
       Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
       if (mlistAppInfo != null) {
           mlistAppInfo.clear();
           for (ResolveInfo reInfo : resolveInfos) {
               String activityName = reInfo.activityInfo.name; // 鑾峰緱璇ュ簲鐢ㄧ▼搴忕殑鍚姩Activity鐨刵ame
               String pkgName = reInfo.activityInfo.packageName; // 鑾峰緱搴旂敤绋嬪簭鐨勫寘鍚?
               String appLabel = (String) reInfo.loadLabel(pm); // 鑾峰緱搴旂敤绋嬪簭鐨凩abel
               Drawable icon = reInfo.loadIcon(pm); // 鑾峰緱搴旂敤绋嬪簭鍥炬爣
               // 涓哄簲鐢ㄧ▼搴忕殑鍚姩Activity 鍑嗗Intent
               Intent launchIntent = new Intent();
               launchIntent.setComponent(new ComponentName(pkgName,
                       activityName));
               // 鍒涘缓涓€涓狝ppInfo瀵硅薄锛屽苟璧嬪€?
               AppInfo appInfo = new AppInfo();
               appInfo.setAppLabel(appLabel);
               appInfo.setPkgName(pkgName);
               appInfo.setAppIcon(icon);
               appInfo.setIntent(launchIntent);
               mlistAppInfo.add(appInfo); // 娣诲姞鑷冲垪琛ㄤ腑
           }
       }
   }


}

<?xml version="1.0"?>


-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world"/>


</RelativeLayout>

<?xml version="1.0" encoding="UTF-8"?>


-<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/listviewApp"/>


</LinearLayout>


<?xml version="1.0" encoding="UTF-8"?>


-<LinearLayout android:layout_height="50dip" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">


<ImageView android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="@+id/imgApp"/>




-<RelativeLayout android:layout_height="40dip" android:layout_width="fill_parent" android:layout_marginLeft="10dip">


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvLabel" android:text="AppLable : "/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvAppLabel" android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700" android:layout_toRightOf="@id/tvLabel"/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvName" android:text="包名:" android:layout_below="@id/tvLabel"/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvPkgName" android:textColor="#FFD700" android:layout_below="@id/tvAppLabel" android:layout_alignLeft="@id/tvAppLabel"/>


</RelativeLayout>


</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值