58程序管理器的简单UI

最终实现的效果如图:


实现了:

1.获取了手机内存可用空间和SD卡可用空间

2.获得了手机安装的应用程序的信息,比如应用程序的名称,应用程序的图标。

开始加载数据的时候,有个ProgressBar提示用户当前正在加载数据:

布局代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f0eb"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#2FD5B9"
        android:gravity="center"
        android:text="软件管理"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_avail_rom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:textColor="#000000"
            />

        <TextView
            android:id="@+id/tv_avail_sd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:textColor="#000000"
             />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/ll_loading"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="invisible" >

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="正在加载..." />
        </LinearLayout>

        <ListView
            android:id="@+id/lv_app_manager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:cacheColorHint="#00000000"
            android:divider="#D4D4D4"
            android:dividerHeight="1px" />
    </FrameLayout>

</LinearLayout>

应用程序信息的业务bean:

package com.ustc.mobilemanager.domain;

import android.graphics.drawable.Drawable;

/**
 * 应用程序信息的业务bean
 * @author 
 *
 */
public class AppInfo {
	private Drawable icon;
	private String name;
	private String packname;
	private boolean inRom;
	private boolean userApp;
	public Drawable getIcon() {
		return icon;
	}
	public void setIcon(Drawable icon) {
		this.icon = icon;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPackname() {
		return packname;
	}
	public void setPackname(String packname) {
		this.packname = packname;
	}
	public boolean isInRom() {
		return inRom;
	}
	public void setInRom(boolean inRom) {
		this.inRom = inRom;
	}
	public boolean isUserApp() {
		return userApp;
	}
	public void setUserApp(boolean userApp) {
		this.userApp = userApp;
	}
	@Override
	public String toString() {
		return "AppInfo [icon=" + icon + ", name=" + name + ", packname="
				+ packname + ", inRom=" + inRom + ", userApp=" + userApp + "]";
	}
	
	

}

业务方法,提供手机里面安装的应用程序的信息:

package com.ustc.mobilemanager.engine;

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

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.ustc.mobilemanager.domain.AppInfo;

/**
 * 业务方法,提供手机里面安装的应用程序的信息
 * 
 * @author
 * 
 */
public class AppInfoProvider {
	/**
	 * 获取所有的安装的应用程序的信息
	 * 
	 * @param context
	 * @return
	 */
	public static List<AppInfo> getAppInfos(Context context) {
		PackageManager pm = context.getPackageManager();
		// 所有的安装在系统上的应用程序包信息
		List<PackageInfo> packInfos = pm.getInstalledPackages(0);
		List<AppInfo> appInfos = new ArrayList<AppInfo>();
		for (PackageInfo packageInfo : packInfos) {
			String packageName = packageInfo.packageName;
			Drawable icon = packageInfo.applicationInfo.loadIcon(pm);
			String name = packageInfo.applicationInfo.loadLabel(pm).toString();
			AppInfo appInfo = new AppInfo();
			appInfo.setIcon(icon);
			appInfo.setName(name);
			appInfos.add(appInfo);
		}

		return appInfos;
	}

}
注意这个工具方法的使用。


ListView的Item的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp" >

    <ImageView
        android:id="@+id/iv_app_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        />

    <TextView
        android:id="@+id/tv_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/iv_app_icon"
        android:textColor="#000000"
        android:layout_marginLeft="5dp"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_app_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:layout_toRightOf="@id/iv_app_icon"
        android:textColor="#88000000"
        android:layout_marginLeft="5dp"
        android:textSize="18sp" />

</RelativeLayout>

加载数据:

package com.ustc.mobilemanager;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ustc.mobilemanager.domain.AppInfo;
import com.ustc.mobilemanager.engine.AppInfoProvider;

public class AppManagerActivity extends Activity {

	private ListView lv_app_manager;
	private LinearLayout ll_loading;
	private TextView tv_avail_rom, tv_avail_sd;
	/*
	 * 所有的应用程序包信息
	 */
	private List<AppInfo> appInfos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_app_manager);
		lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);
		ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
		tv_avail_sd = (TextView) findViewById(R.id.tv_avail_sd);
		tv_avail_rom = (TextView) findViewById(R.id.tv_avail_rom);

		long availSpaceSD = getAvailSpace(Environment
				.getDownloadCacheDirectory().getAbsolutePath());

		long availSpaceRom = getAvailSpace(Environment.getDataDirectory()
				.getAbsolutePath());

		tv_avail_sd.setText("SD卡可用空间:"
				+ Formatter.formatFileSize(this, availSpaceSD));
		tv_avail_rom.setText("内存可用空间:"
				+ Formatter.formatFileSize(this, availSpaceRom));

		ll_loading.setVisibility(View.VISIBLE);
		new Thread() {
			public void run() {
				appInfos = AppInfoProvider.getAppInfos(AppManagerActivity.this);
				// 加载ListView的数据适配器器(放在UI线程中执行)
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						lv_app_manager.setAdapter(new AppManagerAdapter());
						ll_loading.setVisibility(View.INVISIBLE);
					}
				});

			};
		}.start();
	}

	/**
	 * 获取某个目录的可用空间
	 * 
	 * @param path
	 * @return
	 */
	private long getAvailSpace(String path) {
		StatFs statFs = new StatFs(path);
		// 获取分区的个数
		// int blockCount = statFs.getBlockCount();
		// 获取分区的大小
		long blockSize = statFs.getBlockSize();
		// 获取可用的分区的个数
		long availableBlocks = statFs.getAvailableBlocks();
		return blockSize * availableBlocks;
	}

	private class AppManagerAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return appInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder;
			if (convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(getApplicationContext(),
						R.layout.list_item_appinfo, null);
				holder = new ViewHolder();
				holder.icon = (ImageView) view.findViewById(R.id.iv_app_icon);
				holder.tv_location = (TextView) view
						.findViewById(R.id.tv_app_location);
				holder.tv_name = (TextView) view.findViewById(R.id.tv_app_name);

				view.setTag(holder);

			}
			AppInfo appInfo = appInfos.get(position);

			holder.icon.setImageDrawable(appInfo.getIcon());
			holder.tv_name.setText(appInfo.getName());

			return view;

		}

	}

	static class ViewHolder {
		TextView tv_name;
		TextView tv_location;
		ImageView icon;
	}
}
相关文章
相关标签/搜索