[mobilesafe] 01_系统所有应用程序信息界面展示

Android 4.0

系统所有应用程序信息界面展示

关键技术点:
1)ListView:两个优化
a)convertView:复用以前的view
b)ViewHolder:保存获取到的组件的引用
2)Handler+Message,更新界面,设置数据到界面显示出来
3)一行显示不下,滚动显示 FocusedTextView
android:ellipsize="marquee"
android:singleLine="true"
核心代码:
package cn.zengfansheng.mobilesafe;
 
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.zengfansheng.mobilesafe.domain.AppInfo;
import cn.zengfansheng.mobilesafe.engine.AppInfoProvider;
import cn.zengfansheng.mobilesafe.ui.FocusedTextView;
 /**
* 17、软件管理Activity
*
* @author hacket
*/

public class AppManageActivity extends Activity {
 
    private TextView tv_rom_available;//rom可用空间
    private TextView tv_sdcard_available;//sdcard可用空间
    private ListView lv_app_manage;
    private LinearLayout ll_app_data;
 
    private List<AppInfo> appInfos;//装所有应用程序信息的ListView
    private ViewHolder viewHolder;// 提升性能的,将查找到的组件存起来
 
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ll_app_data.setVisibility(LinearLayout.INVISIBLE);
            lv_app_manage.setAdapter(new AppManageAdapter());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_app_manager);

        tv_rom_available = (TextView) this.findViewById(R.id.tv_available_internal_storage);
        tv_sdcard_available = (TextView) this.findViewById(R.id.tv_available_sdcard_storage);
        ll_app_data = (LinearLayout) this.findViewById(R.id.ll_app_data_fill);
        lv_app_manage = (ListView) this.findViewById(R.id.lv_app_list);
 
        // 获取目录时注意:
        // android 1.5 1.6 2.1 /sdcard
        // android 2.2 ~4.1 /mnt/sdcard
        // android 4.2 /mnt/sdcard01
        // /mnt/storage/sdcard01
 
        // a)rom可用
        tv_rom_available.setText(getResources().getString(R.string.internal_storage_available)
                +":"+getAvailableStorage(Environment.getDataDirectory().getAbsolutePath()));
        // b)sdcard可用
        tv_sdcard_available.setText(getResources().getString(R.string.sdcard_storage_available)
                + "" + getAvailableStorage(Environment.getExternalStorageDirectory().getAbsolutePath()));
 
        fillData();
    }
 
    /**
     * 4、展示应用程序信息的Adapter
     *
     * @author hacket
     */

    private class AppManageAdapter extends BaseAdapter {
 
        @Override
        public int getCount() {
            if (appInfos != null) {
                return appInfos.size();
            }
            return 0;
        }
        @SuppressWarnings("unused")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                                                                                                                     
            View view = null;
            if (view != null && view instanceof RelativeLayout) {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }else {
                view = View.inflate(getApplicationContext(), R.layout.listview_appinfo_item, null);
 
                viewHolder = new ViewHolder();
                viewHolder.iv_app_icon = (ImageView) view.findViewById(R.id.iv_app_icon);
                viewHolder.tv_app_name = (FocusedTextView) view.findViewById(R.id.tv_app_name);
                viewHolder.tv_app_install_location = (TextView) view.findViewById(R.id.tv_app_install_location);
                view.setTag(viewHolder);
            }
 
            AppInfo appInfo = appInfos.get(position);
            viewHolder.iv_app_icon.setImageDrawable(appInfo.getAppIcon());
            viewHolder.tv_app_name.setText(appInfo.getAppName());
            boolean isInRom = appInfo.getIsInRom();
            if (isInRom) {// 在手机内存
                viewHolder.tv_app_install_location.setText("内部存储");
            } else {// 在外部存储卡
                viewHolder.tv_app_install_location.setText("外部存储");
            }
            return view;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
 
    private class ViewHolder {
        ImageView iv_app_icon;// 应用程序图标
        FocusedTextView tv_app_name;// 应用程序名
        TextView tv_app_install_location;// 应用程序安装位置
    }
    /**
     * 2、将应用程序的信息展现到界面上去
     */

    private void fillData() {
 
        // 设置等待获取数据界面可见
        ll_app_data.setVisibility(LinearLayout.VISIBLE);
        // 获取应用程序信息
        appInfos = AppInfoProvider.getAppInfos(this);
        // 发送消息更新界面数据
        handler.sendEmptyMessage(0);
    }
 
    /**
     * 1、获取某个目录的大小
     * @param path要判断的路径
     * @return 返回可用空间
     */

    public String getAvailableStorage(String path) {
        StatFs statFs = new StatFs(path);
        int countSize = statFs.getAvailableBlocks() * statFs.getBlockSize();
        String formatFileSize = Formatter.formatFileSize(this, countSize);
        return formatFileSize;
    }
}
问题1:如果某一个应用的名称过长,一列显示不出来
解决: 自定义滚动文本 ,只显示一列,其余的部分,滚动显示 

 
<cn.zengfansheng.mobilesafe.ui.FocusedTextView
     android:id="@+id/tv_app_name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_marginLeft="5dip"
     android:layout_marginTop="5dip"
     android:layout_toRightOf="@+id/iv_app_icon"
     android:ellipsize="marquee"
     android:singleLine="true"
     android:textColor="#000000"
     android:textSize="20sp" />

解决后: