---------------------------------------------------------------------------------------------------------------------------------------------------
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
TextView
tv_flag
;
private
LinearLayout
loading
;
private
List<AppInfoBean>
appInfoList
;
private
List<AppInfoBean>
userAppList
;
private
List<AppInfoBean>
sysAppList
;
private
MyAdapter
adapter
;
private
AppInfoBean
appInfoBean
;
private
Handler
handler
=
new
Handler(){
public
void
handleMessage(android.os.Message msg) {
Log. i(
TAG
,
"进入handler..."
);
loading
.setVisibility(View.
INVISIBLE
);
//移除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
);
appInfoList
=
new
ArrayList<AppInfoBean>();
userAppList
=
new
ArrayList<AppInfoBean>();
sysAppList
=
new
ArrayList<AppInfoBean>();
//找到关心的控件
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
);
tv_flag
=(TextView) findViewById(R.id.
tv_flag
);
//给TextView设置值
tv_avail_rom
.setText(
"内存可用空间:"
+getAvailableSpace(Environment.getDataDirectory ().getAbsolutePath()));
tv_avail_sd
.setText(
"SD卡可用空间:"
+getAvailableSpace(Environment. getExternalStorageDirectory().getAbsolutePath()));
//给lv设置滚动监听事件,实现应用类型提示的 tv变化效果
lv_app_manage
.setOnScrollListener(
new
OnScrollListener() {
@Override
public
void
onScrollStateChanged(AbsListView view,
int
scrollState) {
//
TODO
Auto-generated method stub
}
@Override
public
void
onScroll(AbsListView view,
int
firstVisibleItem,
int
visibleItemCount,
int
totalItemCount) {
//
TODO
Auto-generated method stub
if
(firstVisibleItem>(
userAppList
.size()+1)){
tv_flag
.setText(
"系统应用:"
+
sysAppList
.size());
}
else
{
tv_flag
.setText(
"用户应用:"
+
userAppList
.size());
}
}
});
//填充数据到集合
fillDateToLIst();
}
/**
* 填充数据到集合的方法
*/
public
void
fillDateToLIst(){
loading
.setVisibility(View.
VISIBLE
);
//loading可见
new
Thread(){
public
void
run() {
appInfoList
= AppManager.getAppInfo(AppManagerActivity.
this
);
for
(AppInfoBean appInfo:
appInfoList
){
if
(appInfo.isUserApp()){
userAppList
.add(appInfo);
//用户应用集合
}
else
{
sysAppList
.add(appInfo);
//系统应用集合
}
}
handler
.sendEmptyMessage(0);
};
}.start();
}
/**
* 创建适配器
*/
public
class
MyAdapter
extends
BaseAdapter{
@Override
public
int
getCount() {
return
userAppList
.size()+1+
sysAppList
.size()+1;
}
@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
=(FcousedTextView) view.findViewById(R.id.
tv_appname
);
holder.
tv_installlocation
=(TextView) view.findViewById(R.id.
tv_installlocation
);
view.setTag(holder);
//将holder追加到view上
}
//初始化化控件
if
(position==0){
TextView tv_user=
new
TextView(getApplicationContext());
tv_user.setText(
"用户应用:"
+
userAppList
.size());
tv_user.setTextColor(Color.
BLACK
);
tv_user.setBackgroundColor(color.
gray
);
return
tv_user;
}
else
if
(position==(
userAppList
.size()+1)){
TextView tv_app=
new
TextView(getApplicationContext());
tv_app.setText(
"系统应用:"
+
sysAppList
.size());
tv_app.setTextColor(Color.
BLACK
);
tv_app.setBackgroundColor(color.
gray
);
return
tv_app;
}
else
if
(position<=
userAppList
.size()){
//从集合中得到用户应用
int
newPosition=position-1;
appInfoBean
=
userAppList
.get(newPosition);
holder.
iv_appicon
.setImageDrawable(
appInfoBean
.getIcon());
holder.
tv_appname
.setText(
appInfoBean
.getAppName());
if
(
appInfoBean
.isRoomApp()){
holder.
tv_installlocation
.setText(
"存储位置:内存"
);
}
else
{
holder.
tv_installlocation
.setText(
"存储位置:外部存储"
);
}
}
else
{
int
newPosition=position-1-
userAppList
.size()-1;
appInfoBean
=
sysAppList
.get(newPosition);
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
;
FcousedTextView
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);
}
}