黑马程序员技术交流社区
标题:
手机应用管理2
[打印本页]
作者:
woshiku
时间:
2015-9-10 08:55
标题:
手机应用管理2
package com.woshiku.deleapp.activity;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.woshikju.deleapp.R;
import com.woshiku.deleapp.adapter.ManagerAppAdapter;
import com.woshiku.deleapp.domian.AppInfo;
import com.woshiku.deleapp.enginee.AppInfos;
@SuppressLint({ "NewApi", "HandlerLeak" })
public class ManagerApp extends Activity implements OnScrollListener, OnItemClickListener, OnClickListener, OnItemLongClickListener{
private TextView phone_rom;
private TextView sd_rom;
private TextView app_tip;
private ListView appList;
private MyHandler myHandler;
private ArrayList<AppInfo> appInfos;
private AppInfo clickAppInfo;
private ArrayList<AppInfo> userAppInfos;
private ArrayList<AppInfo> systemAppInfos;
private ManagerAppAdapter manAdapter;
private PopupWindow popupWindow;
private PopupWindow waitWindow =null;
//private LinearLayout app_manager_parent;
private UninstallReceiver receiver = null;
private int selectedPosition;
Context context;
private void initWaitWindow(){
if(waitWindow == null){
View view = View.inflate(context, R.layout.popup_wait, null);
waitWindow = new PopupWindow(view, -2, -2);
waitWindow.showAtLocation(findViewById(R.id.phone_rom), Gravity.NO_GRAVITY, 0, 0);
}
}
private void WaitWindowDismss() {
if (waitWindow != null && waitWindow.isShowing()) {
waitWindow.dismiss();
waitWindow = null;
}
}
private void initViews(){
myHandler = new MyHandler();
context = ManagerApp.this;
System.out.println("初始化");
Log.e("inittest", "初始化");
//app_manager_parent = (LinearLayout)findViewById(R.id.phone_manager_parent);
phone_rom = (TextView)findViewById(R.id.phone_rom);
sd_rom = (TextView)findViewById(R.id.sd_rom);
app_tip = (TextView)findViewById(R.id.app_tip);
appList = (ListView)findViewById(R.id.app_list);
appList.setOnScrollListener(this);
appList.setOnItemClickListener(this);
appList.setOnItemLongClickListener(this);
if(receiver == null){
receiver = new UninstallReceiver();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
registerReceiver(receiver, intentFilter);
}
}
private void initDatas(){
new Thread(){
public void run(){
//initWaitWindow();
//sendMessage(5, null);
appInfos = (ArrayList<AppInfo>) AppInfos.getAppInfos(context);
userAppInfos = new ArrayList<AppInfo>();
systemAppInfos = new ArrayList<AppInfo>();
for(AppInfo appInfo:appInfos){
if(appInfo.isAppSource()){
userAppInfos.add(appInfo);
}else{
systemAppInfos.add(appInfo);
}
}
sendMessage(2, "");
//sendMessage(6, null);
}
}.start();
}
private void popupWindowDismiss() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
}
private void sendMessage(int what ,String info){
Message msg = new Message();
msg.what = what;
Bundle bd = new Bundle();
bd.putString("info", info);
msg.setData(bd);
myHandler.sendMessage(msg);
}
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manager_app);
initViews();
initDatas();
long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();
long sd_freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
sendMessage(0, "手机内存:"+Formatter.formatFileSize(context, rom_freeSpace));
sendMessage(1, "SD卡容量:"+Formatter.formatFileSize(context, sd_freeSpace));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(receiver != null){
unregisterReceiver(receiver);
}
}
private class MyHandler extends Handler{
public void handleMessage(Message msg){
switch(msg.what){
case 0:
phone_rom.setText(msg.getData().getString("info"));
break;
case 1:
sd_rom.setText(msg.getData().getString("info"));
break;
case 2:
manAdapter = new ManagerAppAdapter(context,userAppInfos,systemAppInfos);
appList.setAdapter(manAdapter);
break;
case 3:
app_tip.setText(msg.getData().getString("info"));
break;
case 4:
if ( selectedPosition< userAppInfos.size() + 1) {
//把多出来的特殊的条目减掉
userAppInfos.remove(selectedPosition - 1);
} else {
int location = userAppInfos.size() + 2;
systemAppInfos.remove(selectedPosition - location);
}
manAdapter.notifyDataSetChanged();
break;
case 5:
initWaitWindow();
break;
case 6:
WaitWindowDismss();
break;
}
}
}
class UninstallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("不安装");
Log.e("inittest", "初始化1");
sendMessage(4, null);
}
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
System.out.println("firstVis"+arg1);
popupWindowDismiss();
if(userAppInfos !=null||systemAppInfos != null){
if(arg1<=userAppInfos.size()+1){
sendMessage(3, "用户程序(" + userAppInfos.size() + ")");
}else{
sendMessage(3, "系统程序(" + systemAppInfos.size() + ")");
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
// TODO Auto-generated method stub
//popupWindowDismiss();
if(userAppInfos !=null||systemAppInfos != null){
//获取到当前点击的item对象
Object obj = appList.getItemAtPosition(position);
if (obj != null && obj instanceof AppInfo) {
clickAppInfo = (AppInfo) obj;
selectedPosition = position;
View contentView = View.inflate(context, R.layout.popup_item_1, null);
LinearLayout ll_uninstall = (LinearLayout) contentView.findViewById(R.id.ll_uninstall);
LinearLayout ll_start = (LinearLayout) contentView.findViewById(R.id.ll_start);
ll_uninstall.setOnClickListener(this);
ll_start.setOnClickListener(this);
popupWindowDismiss();
// -2表示包裹内容
popupWindow = new PopupWindow(contentView, -2, view.getHeight());
//需要注意:使用PopupWindow 必须设置背景。不然没有动画
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
int[] location = new int[2];
//获取view展示到窗体上面的位置
view.getLocationInWindow(location);
popupWindow.showAtLocation(parent, Gravity.LEFT + Gravity.TOP, view.getWidth() - popupWindow.getWidth(), location[1]);
ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(500);
contentView.startAnimation(sa);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.ll_uninstall:
Intent uninstall_localIntent = new Intent("android.intent.action.DELETE", Uri.parse("package:" + clickAppInfo.getAppPackageName()));
startActivity(uninstall_localIntent);
popupWindowDismiss();
break;
case R.id.ll_start:
Intent start_localIntent = this.getPackageManager().getLaunchIntentForPackage(clickAppInfo.getAppPackageName());
this.startActivity(start_localIntent);
popupWindowDismiss();
break;
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
long arg3) {
// TODO Auto-generated method stub
return false;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2