获取手机应用listview显示

来源:互联网 发布:c语言关键字是什么 编辑:程序博客网 时间:2024/04/29 08:45

1、新建app_manager.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="55dip"        android:background="#ff11caee"        android:gravity="center_vertical|center_horizontal"        android:orientation="vertical" >        <TextView            android:id="@+id/tv_app_manager_title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="所有程序"            android:textColor="@android:color/white"            android:textSize="25sp" />    </LinearLayout>    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/lv_app_manager"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="1dip"           >        </ListView>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="center_vertical|center_horizontal"             android:orientation="vertical"             android:visibility="invisible"                  android:id="@+id/ll_app_loading">            <ProgressBar                 android:id="@+id/pb_app_manager"            android:layout_width="60dip"            android:layout_height="60dip"                 />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="正在加载应用程序"                android:textColor="@android:color/white" />        </LinearLayout>    </FrameLayout></LinearLayout>

2、新建AppManagerActivity

public class AppManagerActivity extends Activity implements OnClickListener {    protected static final int GET_ALL_APP_FINISHI = 80;    private ListView lv_app_manager;    private LinearLayout ll_app_loading;    private AppInfoProvider provider;    private List<AppInfo> appinfos;    private AppManagerAdapter adapter;    private PopupWindow localPopupWindow;    private String TAG = "AppManagerActivity";    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            switch (msg.what) {            case GET_ALL_APP_FINISHI:                ll_app_loading.setVisibility(View.INVISIBLE);                // 把数据设置给listview的数组适配器                adapter = new AppManagerAdapter(appinfos,                        AppManagerActivity.this);                lv_app_manager.setAdapter(adapter);                break;            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.app_manager);        lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);        ll_app_loading = (LinearLayout) findViewById(R.id.ll_app_loading);        ll_app_loading.setVisibility(View.VISIBLE);        new Thread() {            public void run() {                provider = new AppInfoProvider(AppManagerActivity.this);                appinfos = provider.getAllApps();                Message msg = new Message();                msg.what = GET_ALL_APP_FINISHI;                handler.sendMessage(msg);            };        }.start();        lv_app_manager.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                dismissPopupWindow();                // 获取当前view对象在窗体中的位置                int[] arrayOfInt = new int[2];                view.getLocationInWindow(arrayOfInt);                int i = arrayOfInt[0] + 60;                int j = arrayOfInt[1];                AppInfo info = (AppInfo) lv_app_manager                        .getItemAtPosition(position);                String packname = info.getPackname();                 TextView tv=new TextView(AppManagerActivity.this);                 tv.setTextSize(20);                 tv.setTextColor(Color.RED);                 tv.setText(packname);                localPopupWindow = new PopupWindow(popupView, 230, 70);                // 一定要给popup设置背景颜色                Drawable background = new ColorDrawable(Color.TRANSPARENT);                // Drawable background =                // getResources().getDrawable(R.drawable.local_popup_bg);                localPopupWindow.setBackgroundDrawable(background);                localPopupWindow.showAtLocation(view, Gravity.LEFT                        | Gravity.TOP, i, j);                ll.startAnimation(sa);            }        });        lv_app_manager.setOnScrollListener(new OnScrollListener() {            @Override            public void onScrollStateChanged(AbsListView view, int scrollState) {                dismissPopupWindow();            }            @Override            public void onScroll(AbsListView view, int firstVisibleItem,                    int visibleItemCount, int totalItemCount) {                dismissPopupWindow();            }        });    }    private void dismissPopupWindow() {        if (localPopupWindow != null) {            localPopupWindow.dismiss();            localPopupWindow = null;        }    }    }}

3、angine中方法AppInfoProvider

public class AppInfoProvider {    private Context context;    private PackageManager packageManager;    private String TAG="AppInfoProvider";public AppInfoProvider(Context context) {        this.context = context;        packageManager=context.getPackageManager();    }//  返回当前手机里面所有手机应用程序 public List<AppInfo> getAllApps(){     List<AppInfo> appInfos=new ArrayList<AppInfo>();     List<PackageInfo> packageInfos= packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);     for (PackageInfo info: packageInfos) {//增强的for循环来遍历         AppInfo myApp=new AppInfo();        String packageName=info.packageName;//a        myApp.setPackname(packageName);        Log.i(TAG, "packname"+packageName);        ApplicationInfo appinfo=info.applicationInfo;//a        Drawable icon=appinfo.loadIcon(packageManager);//a        myApp.setIcon(icon);        String appname=appinfo.loadLabel(packageManager).toString();//a        myApp.setAppname(appname);        Log.i(TAG, "appname"+appname);        if (filterApp(appinfo)) {            Log.i(TAG, "用户应用");            myApp.setSystemApp(false);        }else {            Log.i(TAG, "系统应用");            myApp.setSystemApp(true);        }        appInfos.add(myApp);    }    return appInfos; }// 判断某个应用程序是不是三方的应用程序 public boolean filterApp(ApplicationInfo info){     if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0) {         return true;    }else if ((info.flags & ApplicationInfo.FLAG_SYSTEM)==0) {         return true;    }    return false; }}

4、domain实体AppInfo

public class AppInfo {private Drawable icon;private String appname;private String packname;private boolean isSystemApp;public Drawable getIcon() {    return icon;}public void setIcon(Drawable icon) {    this.icon = icon;}public String getAppname() {    return appname;}public void setAppname(String appname) {    this.appname = appname;}public String getPackname() {    return packname;}public void setPackname(String packname) {    this.packname = packname;}public boolean isSystemApp() {    return isSystemApp;}public void setSystemApp(boolean isSystemApp) {    this.isSystemApp = isSystemApp;}}

5、Adapter 中AppManagerAdapter

public class AppManagerAdapter extends BaseAdapter {    private List<AppInfo> appinfos;    private static ImageView iv;    private static TextView tv;    private Context context;    private String TAG="AppManagerAdapter";    public AppManagerAdapter(List<AppInfo> appinfos, Context context) {        this.appinfos = appinfos;        this.context = context;    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return appinfos.size();    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return appinfos.get(position);    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    // 转化view对象 convertView    @Override    public View getView(int position, View convertView, ViewGroup parent) {        AppInfo info = appinfos.get(position);        View view;        if (convertView == null) {            Log.i(TAG, "通过资源");            view = View.inflate(context, R.layout.app_item, null);        } else {            Log.i(TAG, "使用历史缓存view对象");            view = convertView;        }        iv = (ImageView) view.findViewById(R.id.iv_icon);        tv = (TextView) view.findViewById(R.id.tv_app_name);        iv.setImageDrawable(info.getIcon());        tv.setText(info.getAppname());        return view;    }}

6、app_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="65dip"    android:background="#000000"    android:orientation="horizontal"    android:gravity="center_vertical">    <ImageView        android:id="@+id/iv_icon"        android:layout_width="40dip"        android:layout_height="40dip"        android:scaleType="fitXY"        android:src="@drawable/ic_launcher" >    </ImageView>    <TextView        android:id="@+id/tv_app_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:maxLines="4"        android:singleLine="true"        android:text="功能名字"        android:textSize="17sp"         android:layout_marginLeft="5dip"        android:textColor="#ffffff">    </TextView></LinearLayout>
0 0
原创粉丝点击