Dialog顯示手機android安裝的app資訊

來源:互聯網
上載者:User

標籤:android開發   gridview   介面   

最近做功能開發時候需要顯示當前手機安裝的app資訊.這裡我使用Dialog來顯示.如下:


如上所示,這個dialog顯示了當前手機安裝的app,當點擊一個功能表項目時候就會在上面顯示一個上鎖的圖案(如:視頻/相機/連絡人).這時一個可以對app上鎖的設計介面.

首先來看看dialog的建立:

private void Init_create_dialog(){if(null == show_app_dialog){show_app_dialog = new Dialog(getActivity()){protected void onStop() {super.onStop();FingerPrint_Utils.FingerPrint_save_LockApp(getActivity());}};show_app_dialog.setContentView(R.layout.finger_gride_app_choose);show_app_dialog.setTitle(R.string.finger_print_set_unlock_app_title);GridView gvApp = ((GridView)show_app_dialog.findViewById(R.id.gv_app));final List<FingerPrintApplicationInfo> list = FingerPrint_Utils.get_LockApplicationInfoList();gvApp.setAdapter(new AppChooseAdapter(list));gvApp.setOnItemClickListener(new AdapterView.OnItemClickListener(){public void onItemClick(AdapterView<?> parent, View view, int position, long id){FingerPrintApplicationInfo Fingerappinfo = (FingerPrintApplicationInfo)parent.getItemAtPosition(position);ResolveInfo appinfo = Fingerappinfo.launcher_activity;//CharSequence name = appinfo.loadLabel(package_manager);AppChooseAdapter.ViewHolder hold = (AppChooseAdapter.ViewHolder) view.getTag();if(Fingerappinfo.locked){hold.app_icon.setImageResource(0);Fingerappinfo.locked = false;FingerPrint_Utils.FingerPrint_Dele_LockApp(appinfo.activityInfo.packageName);//activityInfo.packageName}else{hold.app_icon.setImageResource(R.drawable.lock_app_icon);Fingerappinfo.locked = true;FingerPrint_Utils.FingerPrint_AddNew_LockApp(appinfo.activityInfo.packageName);}//selected_str = appinfo.packageName;//if(DEBUG) Log.d(TAG,"FingerPrintSettingMain:  name="+name+"; appinfo.packageName="+appinfo.packageName);//show_app_dialog.dismiss();}});/*            show_app_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {                public void onDismiss(final DialogInterface dialog) {FingerPrint_Utils.FingerPrint_save_LockApp(getActivity());                }            });*/show_app_dialog.getWindow().setCloseOnTouchOutside(true);show_app_dialog.setCanceledOnTouchOutside(true);}}

再看看其layout的檔案:finger_gride_app_choose.xml

<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gv_app"android:layout_width="match_parent" android:layout_height="match_parent"    android:numColumns="3"     android:padding="10dp"    android:verticalSpacing="10dp"    android:horizontalSpacing="10dp"    android:gravity="center"    />
實際上就是一個GridView,所以整個介面的重點其實就是這個GridView的Adapter設計.所以看看這個Adapter:AppChooseAdapter

 class AppChooseAdapter extends BaseAdapter{    private List<FingerPrintApplicationInfo> applicationInfos;    private PackageManager pm;class ViewHolder { TextView app_name; ImageView app_icon; //ImageView locked_icon;}    public AppChooseAdapter(List<FingerPrintApplicationInfo> list){        applicationInfos = list;    }    public int getCount(){        return applicationInfos.size();    }    public Object getItem(int position){        return applicationInfos.get(position);    }    public long getItemId(int position){        return position;    }    public View getView(int position, View view, ViewGroup parent){        //PackageManager pm = package_manager;        final ViewHolder hold;        if (view == null){if(DEBUG) Log.d(TAG,"AppChooseAdapter:   (view == null) position="+position);Context context = parent.getContext();view = LayoutInflater.from(context).inflate(R.layout.item_app_gride, null);hold = new ViewHolder();hold.app_icon = (ImageView)view.findViewById(R.id.iv_icon);//hold.locked_icon = (ImageView)view.findViewById(R.id.lock_statu_img);hold.app_name = (TextView)view.findViewById(R.id.tv_name);view.setTag(hold);}else{if(DEBUG) Log.d(TAG,"AppChooseAdapter:   (view != null) position="+position);hold = (ViewHolder) view.getTag();}final int curr_position = position;view.post(new Runnable() {public void run() {//ViewHolder hold = (ViewHolder) view.getTag();PackageManager pm = package_manager;if(hold != null){FingerPrintApplicationInfo aInfo = (FingerPrintApplicationInfo)applicationInfos.get(curr_position);String package_name = aInfo.launcher_activity.activityInfo.packageName;//aInfo.app.packageName;if(aInfo.locked){hold.app_icon.setImageResource(R.drawable.lock_app_icon); }else{hold.app_icon.setImageResource(0); }hold.app_name.setText(aInfo.launcher_activity.loadLabel(pm));hold.app_icon.setBackgroundDrawable(aInfo.launcher_activity.loadIcon(pm)); //image.setImageDrawable(aInfo.app.loadIcon(pm));}}});        return view;    }}
上面已經貼出了基本所有代碼.下面做一下說明:

(1)每一次顯示這個dialog都會一點延時,如何解決這個問題.我有了兩個辦法來解決:

首先,載入手機安裝的app資訊,我會在顯示這個dialog之前就載入好,然後只需要通過FingerPrint_Utils.get_LockApplicationInfoList()來擷取就可以.其中載入app資訊使用AsyncTask來完成的.AsyncTask的使用比較簡單了,這裡我只給出載入app的資訊的代碼:

    private static ArrayList<FingerPrintApplicationInfo> getInstalledLaunchableApps(FingerPrintSettingMain.AppLabelComparator mcomparator){        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);                PackageManager pm = mcomparator.getPackageManager();        List list = pm.queryIntentActivities(mainIntent, 0);//查詢擷取安裝的所有app                ArrayList<FingerPrintApplicationInfo> array = new ArrayList<FingerPrintApplicationInfo>();        ArrayList<FingerPrintApplicationInfo> array1 = new ArrayList<FingerPrintApplicationInfo>();                int size = list.size();        String[] APP_DELE = ALL_APP_DELE_PACKAGENAME;        String[] LOCK_APP_DELE = LOCK_APP_DELE_PACKAGENAME;        //Log.d(TAG,"getInstalledLaunchableApps: size="+size);        boolean has = false;        String locked_packagename = app_locked_PackageName!=null?app_locked_PackageName.toString():null;        ResolveInfo curr;        for (int i=0; i<size; i++){            has = false;            curr = (ResolveInfo)list.get(i);            if(null == curr){                 continue;            }            String str = curr.activityInfo.packageName;            if(!wei_xing_install && 0 == weixing_package.compareTo(str)){//檢測是否安裝                wei_xing_install = true;            }            Iterator iterator = array.iterator();            if(null != APP_DELE){                for(int j = 0; j < APP_DELE.length; j++){//有一些app不需要在這裡顯示,過濾掉一些app                    if(APP_DELE[j].compareTo(str) == 0){                        has = true;                    }                }            }            if(has) continue;                FingerPrintApplicationInfo app = new FingerPrintApplicationInfo();                app.launcher_activity = curr;                if(locked_packagename != null && locked_packagename.indexOf(str)>-1){                     app.locked = true;                }                                array.add(app);                if(null != LOCK_APP_DELE){                    for(int n = 0; n < LOCK_APP_DELE.length; n++){                        if(LOCK_APP_DELE[n].compareTo(str) == 0){                            has = true;                        }                    }                }                if(has) continue;                array1.add(app);        }                Collections.sort(array, mcomparator);        Collections.sort(array1, mcomparator);        if(array1 != null){            unlock_pacakegename_app_dele = array1;        }        return array;    }
即使在顯示dialog之前就載入好app資訊,在顯示dialog時候也會有一個較長的延遲,這是因為載入每一個app的icon和text_name也是需要時間的.所以,如上面的代碼AppChooseAdapter的getView中我使用了view.post(new Runnable())的方法來載入顯示的資訊.

(2)如何?點擊一個app菜單選項時候能夠在上面顯示一個鎖的映像.仔細看上面的源碼可以發現,這裡我使用了ImageView的image和背景來做的.首先ImageView的背景圖片用來顯示app的icon,而ImageView的image來顯示是鎖的圖片.如果上鎖則這個ImageView就設定image:

hold.app_icon.setImageResource(R.drawable.lock_app_icon);

否則:

hold.app_icon.setImageResource(0);




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Dialog顯示手機android安裝的app資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.