您的位置:首页 > 编程语言

我的手机管家(18) 应用管理 主要代码

2016-07-05 12:36 204 查看
我的手机管家(18) 应用管理 主要代码

点击ListView的item跳出一个弹出窗口

/**
*弹出窗口
   *  @param  View pView 是被触发点击事件的View ,
    
*/
protected void showPopupWindow(View pView){
if(mPopupWindow==null){
popupView = View.inflate(this, R.layout.popup_adapter, null);
mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
//设定一个背景,这样的话点击返回键可用消失
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
TextView tvStart = (TextView) popupView.findViewById(R.id.tv_lanuch);
TextView tvUninstall = (TextView) popupView.findViewById(R.id.tv_uninstall);
TextView tvShare=(TextView) popupView.findViewById(R.id.tv_uninstall);

tvStart.setOnClickListener(this);
tvUninstall.setOnClickListener(this);
tvShare.setOnClickListener(this);
   //使用动画来展示弹出窗口
//缩放动画
ScaleAnimation a= new ScaleAnimation(0, 1,0,1,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f
);
a.setDuration(200);

//渐变动画
AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setDuration(200);
animationset = new AnimationSet(false);
animationset.addAnimation(a);
animationset.addAnimation(alpha);

}
mPopupWindow.showAsDropDown(pView,60,-pView.getHeight());
popupView.startAnimation(animationset);
}


弹出窗口界面

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

<TextView
android:id="@+id/tv_lanuch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/img2"
android:text="启动" />
<TextView
android:id="@+id/tv_uninstall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/img1"
android:text="卸载" />
<TextView
android:id="@+id/tv_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/img3"
android:text="分享" />

</LinearLayout>




package com.chb.myphonesave.activity;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
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.os.StatFs;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.chb.myphonesave.R;
import com.chb.myphonesave.entity.AppInfo;
import com.chb.myphonesave.provider.AppInfoProvider;
/**
*应用管理
*/
public class AppManagerActivity extends Activity implements OnClickListener{
private TextView tvMemAvail;//内存可用
private TextView tvSDCard;//SD卡可用
private TextView tvHeader;//头部信息
ListView lv;//列表
LinearLayout  layout;//线性布局
private List<AppInfo> mList;//应用集合
private List<AppInfo> mUserAppList;//用户应用
private List<AppInfo> mSystemAppList;//系统应用
private AppInfo appInfo;//当前被点击的对象
private AppAdapter adapter;

private PopupWindow mPopupWindow;
private View popupView;
private AnimationSet animationset;

private Handler myhHandler = new Handler(){
public void handleMessage(Message msg) {
//刷新数据
adapter = new AppAdapter();
lv.setAdapter(adapter);
tvHeader.setText("应用程序:("+mUserAppList.size()+")");
layout.setVisibility(View.GONE);
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appmanager);
//初始化
tvMemAvail = (TextView) findViewById(R.id.app_tvMemAvail);
tvSDCard = (TextView) findViewById(R.id.app_avial_id);
tvHeader = (TextView) findViewById(R.id.app_tv_header);

lv = (ListView) findViewById(R.id.app_lv_list);
layout = (LinearLayout) findViewById(R.id.app_linearlayout_id);

//获取内存可用空间,手机自带存储空间(手机内存根路径)
tvMemAvail.setText("内存可用:"+getAvaliSpace(Environment.getDataDirectory().getAbsolutePath()));
//SDCard路径
tvSDCard.setText("SDCard可用:"+getAvaliSpace(Environment.getExternalStorageDirectory().getAbsolutePath()));
//初始化数据
initAppData();
//列表添加监听事件
lv.setOnScrollListener(
new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {

}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(mUserAppList!=null&&mSystemAppList!=null){
//已经滑到系统用户了
if(firstVisibleItem>mUserAppList.size()+1){
tvHeader.setText("系统应用:("+mSystemAppList.size()+")");
}else{
tvHeader.setText("用户应用:("+mUserAppList.size()+")");
}
}
}
});
//添加点击事件
lv.setOnItemClickListener(
new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AppInfo info = adapter.getItem(position);
if(info != null){
appInfo=info;
showPopupWindow(view);//此view是被当前点击的view
}
}
});
}
public void initAppData(){
new Thread(){
public void run() {
mList = AppInfoProvider.getAppInfos(AppManagerActivity.this);
mUserAppList = new ArrayList<AppInfo>();
mSystemAppList = new ArrayList<AppInfo>();
//判断是否是系统应用还是用户应用
for (AppInfo info : mList) {
if(info.isUserApp()){//用户应用
mUserAppList.add(info);
}else{//系统应用
mSystemAppList.add(info);
}

}
myhHandler.sendEmptyMessage(0);
}
}.start();
}

/**
*弹出窗口
*/
protected void showPopupWindow(View pView){
if(mPopupWindow==null){
popupView = View.inflate(this, R.layout.popup_adapter, null);
mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
//设定一个背景,这样的话点击返回键可用消失
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
TextView tvStart = (TextView) popupView.findViewById(R.id.tv_lanuch);
TextView tvUninstall = (TextView) popupView.findViewById(R.id.tv_uninstall);
TextView tvShare=(TextView) popupView.findViewById(R.id.tv_uninstall);

tvStart.setOnClickListener(this);
tvUninstall.setOnClickListener(this);
tvShare.setOnClickListener(this);

//缩放动画
ScaleAnimation a= new ScaleAnimation(0, 1,0,1,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f
);
a.setDuration(200);

//渐变动画
AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setDuration(200);
animationset = new AnimationSet(false);
animationset.addAnimation(a);
animationset.addAnimation(alpha);

}
mPopupWindow.showAsDropDown(pView,60,-pView.getHeight());
popupView.startAnimation(animationset);
}
/*
*获取某个路径下的可用空间
*/
private String getAvaliSpace(String path){
StatFs sf=new StatFs(path);
//获取可用存储块容量
long avaliBlocks=sf.getAvailableBlocks();
//获取每一块的容量大小
long blockSize = sf.getBlockSize();
//可用总空间大小为 可用存储块容量*每块容量大小
long size=avaliBlocks*blockSize;
return Formatter.formatFileSize(getApplicationContext(), size);
}
class AppAdapter  extends BaseAdapter{
public int getCount() {
return mUserAppList.size()+mSystemAppList.size()+2;
}
public AppInfo getItem(int position) {
//如果是头部信息
if(position==0||position==mUserAppList.size()+1){
return null;
}
//根据当前的position返回AppInfo应用
if(position<mUserAppList.size()+1){//用户应用
return mUserAppList.get(position-1);
}else{
return mSystemAppList.get(position-mUserAppList.size()-2);
}
}
public long getItemId(int position) {
return position;
}
/**
* 根据位置返回当前的item类型
* 1.头布局
* 2.普通布局
*/
public int getItemViewType(int position) {
if(position==0||position==mUserAppList.size()+1){
return 0;//头布局
}else{
return 1;//普通布局
}
}
/**
* 返回总共有多少类型,该方法必须实现,系统会根据返回的类型判断需要缓存几个ContentView
*/
public int getViewTypeCount() {
return 2;
}
public View getView(int position, View convertView, ViewGroup parent) {
//系统会根据当前的item类型,返回准确的converView,有几种类型,就缓存几个converView
int type=getItemViewType(position);
switch (type) {
case 0:
HeaderHolder headerHolder;
if(convertView==null){
convertView = View.inflate(getApplicationContext(), R.layout.list_appinfo_header,null);
headerHolder = new HeaderHolder();
headerHolder.tvHeader = (TextView) convertView.findViewById(R.id.app_tv_header);
convertView.setTag(headerHolder);
}else{
headerHolder = (HeaderHolder) convertView.getTag();
}
if(position==0){
headerHolder.tvHeader.setText("用户应用:("+mUserAppList.size()+")");
}else{
headerHolder.tvHeader.setText("系统应用:("+mSystemAppList.size()+")");
}
break;
case 1:
ViewHolder holder;
if(convertView==null){
convertView = View.inflate(getApplicationContext(),
R.layout.app_info_adapter, null);
holder = new ViewHolder();
holder.iv= (ImageView) convertView.findViewById(R.id.app_imageView1);
holder.title = (TextView) convertView.findViewById(R.id.app_textView1);
holder.location = (TextView) convertView.findViewById(R.id.app_textView2);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
AppInfo info =getItem(position);
holder.iv.setImageDrawable(info.getIcon());
holder.title.setText(info.getAppName());
if(info.isRom()){//系统内存
holder.location.setText("手机内存");
}else{//用户内存
holder.location.setText("外置内存卡");
}
break;
default:
break;
}
return convertView;
}
}
static class ViewHolder{
public ImageView iv;
public TextView title;
public TextView location;
}
static class HeaderHolder{
public TextView tvHeader;
}
//点击事件
public void onClick(View v) {
if(mPopupWindow!= null){
mPopupWindow.dismiss();//消失
}
switch (v.getId()) {
case R.id.tv_lanuch://启动
launch(appInfo.getPackageName());
break;
case R.id.tv_uninstall://卸载
if(appInfo.isUserApp()){
uninstall(appInfo.getPackageName());
}else{
Toast.makeText(this, "系统应用,需要root权限才可以卸载", Toast.LENGTH_LONG).show();
}
break;
case R.id.tv_share://分享
share(appInfo.getPackageName());
break;
default:
break;
}
}
/**
* 启动应用
*/
public void launch(String packageName){
PackageManager pm = getPackageManager();
//获得app启动界面的intent
Intent intent = pm.getLaunchIntentForPackage(packageName);
startActivity(intent);
}
/**
* 卸载应用
*/
public void uninstall(String packageName){
//跳转到卸载界面
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:"+packageName));
startActivityForResult(intent, 0);
}
/**
* 分享应用
*/
public void share(String packageName){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");//发送文本
intent.putExtra(Intent.EXTRA_TEXT, packageName);
startActivity(intent);
}
public void startActivityForResult(Intent intent, int requestCode) {
initAppData();//卸载完成后,重写刷新界面
super.startActivityForResult(intent, requestCode);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: