public class AppManagerActivity extends Activity {
protected
static
final
String
TAG
=
"AppManagerActivity"
;
private
TextView
tv_avail_rom
;
private
TextView
tv_avail_sd
;
private
ListView
lv_app_manage
;
private
LinearLayout
loading
;
private
List<AppInfoBean>
appInfoList
;
private
MyAdapter
adapter
;
private
Handler
handler
=
new
Handler(){
public
void
handleMessage(android.os.Message msg) {
Log. i(
TAG
,
"进入handler..."
);
loading
.setVisibility(View.
GONE
);
//移除loading
lv_app_manage
.setAdapter(
new
MyAdapter());
Log. i(
TAG
,
"填充"
);
};
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_app_manage
);
//找到关心的控件
tv_avail_rom
=(TextView) findViewById(R.id.
tv_avail_rom
);
tv_avail_sd
=(TextView) findViewById(R.id.
tv_avail_sd
);
lv_app_manage
=(ListView) findViewById(R.id.
lv_app_manage
);
loading
=(LinearLayout) findViewById(R.id.
loading
);
//给TextView设置值
tv_avail_rom
.setText(
"内存可用空间:"
+getAvailableSpace(Environment.getDataDirectory ().getAbsolutePath()));
tv_avail_sd
.setText(
"SD卡可用空间:"
+getAvailableSpace(Environment. getExternalStorageDirectory().getAbsolutePath()));
// appInfoList=new ArrayList<AppInfoBean>();
//填充数据到集合
fillDateToLIst();
}
/**
* 填充数据到集合的方法
*/
public
void
fillDateToLIst(){
loading
.setVisibility(View.
VISIBLE
);
//loading可见
new
Thread(){
public
void
run() {
appInfoList
= AppManager.getAppInfo(AppManagerActivity.
this
);
Log. i(
TAG
,
"集合的大小:"
+
appInfoList
.size());
handler
.sendEmptyMessage(0);
};
}.start();
}
/**
* 创建适配器
*/
public
class
MyAdapter
extends
BaseAdapter{
@Override
public
int
getCount() {
return
appInfoList
.size();
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
View view=
null
;
ViewHolder holder;
if
(convertView!=
null
&& convertView
instanceof
RelativeLayout){
//复用历史view
view=convertView;
holder=(ViewHolder) view.getTag();
//复用孩子的id
}
else
{
//创建新的view
Log. i(
TAG
,
"创建信息的item"
);
view=View. inflate(getApplicationContext(), R.layout.
list_app_item
,
null
);
holder=
new
ViewHolder();
//初始化静态类中控件,找到关心的控件
holder.
iv_appicon
=(ImageView) view.findViewById(R.id.
iv_appicon
);
holder.
tv_appname
=(TextView) view.findViewById(R.id.
tv_appname
);
holder.
tv_installlocation
=(TextView)
view.
findViewById(R.id.
tv_installlocation
);
view.setTag(holder);
//将holder追加到view上
}
AppInfoBean appInfoBean =
appInfoList
.get(position);
holder.
iv_appicon
.setImageDrawable(appInfoBean.getIcon());
holder.
tv_appname
.setText(appInfoBean.getAppName());
if
(
appInfoBean
.isRoomApp()){
holder.
tv_installlocation
.setText(
"存储位置:内存"
);
}
else
{
holder.
tv_installlocation
.setText(
"存储位置:外部存储"
);
}
return
view;
}
@Override
public
Object getItem(
int
position) {
return
null
;
}
@Override
public
long
getItemId(
int
position) {
return
0;
}
}
/**
* 创建静态来初始化条目中的控件
*/
static
class
ViewHolder{
ImageView
iv_appicon
;
TextView
tv_appname
;
TextView
tv_installlocation
;
}
/**
* 获取某个目录的大小
*
@param
path
*
@return
*/
public
String getAvailableSpace(String path){
StatFs statf =
new
StatFs(path);
long
size = statf. getAvailableBlocks()*statf. getBlockSize();
return
Formatter. formatFileSize(
this
, size);
}
}