软件管理(显示系统所有安装软件,系统。用户,并清理)

软件管理(显示系统所有安装软件,系统。用户,并清理)_第1张图片

软件管理(显示系统所有安装软件,系统。用户,并清理)_第2张图片
2.png

写基类的东西
软件管理的主界面

public class SoftmgrActivity extends BaseActivity implements View.OnClickListener{
private CleaeArcView rootView;
private CleaeArcView sdView;
private ProgressBar rootProgress;
private ProgressBar sdProgress;
private TextView rootText;
private TextView sdText;
private TextView tvall;
private TextView tvuser;
private TextView tvsystem;

@Override
protected boolean isShowActionBar() {
    return true;
}

@Override
protected void initData() {
    setTitle("软件管理");
    long rootavailable = MemoryInfoManager.rootavailable();
    long rootlTotal = MemoryInfoManager.rootlTotal();
    long sdtotal = MemoryInfoManager.sdtatal();
    long sdavaiable = MemoryInfoManager.sdavaiable();
    int rootendsweep= (int) (rootlTotal*1.0f/(rootlTotal+sdtotal)*360);
    int sdendsweep= (int) (sdtotal*1.0f/(rootlTotal+sdtotal)*360);
    rootView.setSweepAngle(-rootendsweep);
    rootView.setColor("#3852d2");
    sdView.setSweepAngle(sdendsweep);
    sdView.setColor("#e43b5a");
    int rootpro= (int) ((rootlTotal-rootavailable)*1.0f/rootlTotal*100);
    String roottv = Formatter.formatFileSize(this, (rootlTotal - rootavailable));
    String roottv2 = Formatter.formatFileSize(this, rootlTotal);
    int sdpro= (int) ((sdtotal-sdavaiable)*1.0f/sdtotal*100);
    String sdtv = Formatter.formatFileSize(this, (sdtotal - sdavaiable));
    String sdtv2 = Formatter.formatFileSize(this, sdtotal);
    rootProgress.setProgress(rootpro);
    sdProgress.setProgress(sdpro);
    rootText.setText("内置空间"+roottv+"/"+roottv2);
    sdText.setText("外部空间"+sdtv+"/"+sdtv2);
}

@Override
protected void initView() {
    rootView = getViewById(R.id.myview_softmgr_root);
    sdView = getViewById(R.id.myview_softmgr_sd);
    rootProgress = getViewById(R.id.pb_softmgr_root);
    sdProgress = getViewById(R.id.pb_softmgr_sd);
    rootText = getViewById(R.id.tv_soft_root);
    sdText = getViewById(R.id.tv_soft_sd);
    tvall = getViewById(R.id.tv_soft_all);
    tvuser = getViewById(R.id.tv_soft_user);
    tvsystem = getViewById(R.id.tv_soft_system);
}

@Override
protected int getLayoutId() {
    return R.layout.activity_softmgr;
}

@Override
protected void setListaner() {
    tvall.setOnClickListener(SoftmgrActivity.this);
    tvuser.setOnClickListener(SoftmgrActivity.this);
    tvsystem.setOnClickListener(SoftmgrActivity.this);
}

@Override
public void onClick(View v) {
            Intent intent=new Intent(SoftmgrActivity.this,SoftcleanActivity.class);
            intent.putExtra("id",v.getId());
            startActivity(intent);
}
}

主界面的布局



    
    

    
        
            
            
        
        
            
            
        
    











 

布局中扇形的布局,及自定义控件(com.gfd.phone.view.CleaeArcView)

 public class CleaeArcView extends View {

private RectF rectF;
/** 扫描的角度:决定圆弧的弧长*/
private float sweepAngle = 90;
private Paint mPaint;
/** 状态:0:表示后退 1:前进 */
private int state = 0;
private boolean isRuning;
private int[] back = {5,8,10,12,15,18,12,6};
private int[] go = {8,10,14,18,12,10};
private int indexBack;

public CleaeArcView(Context context) {
    this(context, null);
}

/**
 * 有属性的时候
 */
public CleaeArcView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

/**
 * 有风格的时候
 */
public CleaeArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //初始化操作
    //抗锯齿
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setTextSize(20);
}

/**
 * 测量
 * @param widthMeasureSpec  :既有测量的大小又有测量的规则
 * @param heightMeasureSpec
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //获取宽和高的大小
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    rectF = new RectF(0,0,width,height);
    //设置测量的尺寸
    setMeasuredDimension(width,height);

}

/**
 * 测量
 * @param canvas :画布
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //绘制圆弧:1.决定位置矩形 2。起始角度(顺时针是+)(-90:表示从正上方画)
    //3.true :表示扇形 false:圆弧 4.画笔
    canvas.drawArc(rectF,-90, sweepAngle,true,mPaint);
}

/**
 * 开始开始的角度
 * @param toAngle
 */
public void setStartAngle(final int toAngle){
    this.sweepAngle = toAngle;
    postInvalidate();
}
/**
 * 先从当前的状态回到0的位置,然后再从0的位置转到指定的位置
 * @param toAngle
 */
public void setSweepAngle(final int toAngle){
    if(isRuning)return;
    final Timer timer = new Timer(false);
    final TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            switch (state){
                case 0://后退
                    //先归0
                    isRuning = true;
                    sweepAngle -= back[indexBack++];
                    if(indexBack >= back.length){
                        indexBack = back.length - 1;
                    }
                    if(sweepAngle <=0 ){
                        sweepAngle = 0;
                        state = 1;
                    }
                    postInvalidate();//在子线程中刷新UI
                    //开始到指定的位置
                    break;
                case 1://前进
                    sweepAngle += 10;
                    if(sweepAngle >= toAngle){
                        sweepAngle = toAngle;
                        //结束
                        timer.cancel();
                        state = 0;
                        isRuning = false;
                        indexBack = 0;
                    }
                    postInvalidate();
                    break;
            }

        }
    };
    timer.schedule(timerTask,45,45);

}
public  void setColor(String color){
    mPaint.setColor(Color.parseColor(color));//设置画笔的颜色
    invalidate();
}
 }

主界面布局的进度条,在很多情况下是可以复用的





    
        
        
    




    
        
            
            
        
    



软件清理的界面

public class SoftcleanActivity extends BaseActivity {

public SoftAdapter softAdapter;
public ListView mList;
public List datas=new ArrayList<>();
public List appInfo;
public static CheckBox mCbAllselect;
public AppRecevise recevise;


@Override
protected boolean isShowActionBar() {
    return true;
}

@Override
protected void initData() {
    softAdapter = new SoftAdapter(datas);
    mList.setAdapter(softAdapter);
    Intent intent=getIntent();
    int id=intent.getIntExtra("id",-1);
    switch (id){
        case R.id.tv_soft_all:
            setTitle("所有软件");
            appInfo = Program.getAppInfo();
            break;
        case R.id.tv_soft_user:
            setTitle("用户软件");
            appInfo = Program.getAppUser();
            break;
        case R.id.tv_soft_system:
            setTitle("系统软件");
            appInfo = Program.getAppSystem();
            break;
    }
    softAdapter.refresh(appInfo);
}

@Override
protected void initView() {
    mList = getViewById(R.id.list_soft);
    mCbAllselect = getViewById(R.id.cb_soft_seclect);
    //广播注册
    recevise = new AppRecevise();
    IntentFilter filter=new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
    filter.addDataScheme("package");
    registerReceiver(recevise,filter);
}
@Override
protected void setListaner() {
    mCbAllselect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (RunInfo data : datas) {
                data.setIsselect(mCbAllselect.isChecked());
            }
            softAdapter.notifyDataSetChanged();
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(recevise);
}

@Override
protected int getLayoutId() {
    return R.layout.activity_softclean;
}


public void bt_soft_all(View view){
    for (RunInfo data : datas) {
        if (data.isselect()){
            Intent intent=new Intent(Intent.ACTION_DELETE);
            intent.setData(Uri.parse("package:"+data.getPackname()));
            startActivity(intent);
        }
    }

}
//卸载成功
private class AppRecevise extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (id) {
            case R.id.tv_soft_all:
                appInfo = Program.getAppInfo();
                break;
            case R.id.tv_soft_user:
                appInfo = Program.getAppUser();
                break;
            case R.id.tv_soft_system:
                appInfo = Program.getAppSystem();
                break;
        }
        softAdapter.refresh(appInfo);
        mCbAllselect.setChecked(false);
    }
}

}

清理界面的布局




 
    

获取各种软件的代码

public class Program  {

public static List runInfoList;
public static List allUser;
public static List user;
private static PackageInfo packageInfo;
private static List allAppInfo;
private static List allAppuser;
private static List allAppsystem;

public static List getAllinfo(){
    ActivityManager am= (ActivityManager) App.appContext.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm= App.appContext.getPackageManager();
    List allappinfo = am.getRunningAppProcesses();
    runInfoList = new ArrayList<>();
    allUser= new ArrayList<>();
    user = new ArrayList<>();

        for (ActivityManager.RunningAppProcessInfo appProcessInfo : allappinfo) {
            try{
            //每个进程代表的ID
            int id=appProcessInfo.pid;
            //进程的包名
            String packName=appProcessInfo.processName;
            //进程的级别
            int importance=appProcessInfo.importance;
            //判断是本APP时不显示
            if (packName.equals(App.appContext.getPackageName())){
                continue;
            }
            if (importance>=ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE){
                //得到内存信息
                Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(new int[]{id});
                //得到内存的大小
                long size=memoryInfo[0].getTotalPrivateDirty()*1024;
                //APP的信息
                ApplicationInfo appinfo = pm.getApplicationInfo(packName, 0);
                //APP的图标
                Drawable icon = appinfo.loadIcon(pm);
                //APP的名字
                String appName = appinfo.loadLabel(pm).toString();
                RunInfo runInfo=new RunInfo(appName,icon,packName,size);
                runInfoList.add(runInfo);
                if ((appinfo.flags & appinfo.FLAG_SYSTEM)!=0){
                    allUser.add(runInfo);
                }else {
                     user.add(runInfo);
                }
            }
        }catch (Exception e){
        e.printStackTrace();
    }
        }

    return runInfoList;
}
  public static List getAllUser(){
   getAllinfo();
return allUser;
  }
   public static  List getUser(){
  getAllinfo();
return user;
  }
  //删除软件的代码
  public static void killApp(String name){
   ActivityManager am= (ActivityManager)    App.appContext.getSystemService(Context.ACTIVITY_SERVICE);
   am.killBackgroundProcesses(name);
  }

  public static List getAppInfo(){
   allAppInfo = new ArrayList<>();
   allAppuser = new ArrayList<>();
   allAppsystem = new ArrayList<>();
   PackageManager pm=App.appContext.getPackageManager();
   List inAlldAppInfo = pm.getInstalledApplications(PackageManager.GET_ACTIVITIES
           | PackageManager.MATCH_UNINSTALLED_PACKAGES);
   for (ApplicationInfo applicationInfo : inAlldAppInfo) {
       Drawable icon = applicationInfo.loadIcon(pm);
       String name = applicationInfo.loadLabel(pm).toString();
       String packName = applicationInfo.packageName;
       try {
           packageInfo = pm.getPackageInfo(packName, 0);
       } catch (PackageManager.NameNotFoundException e) {
           e.printStackTrace();
       }
       String versionName = packageInfo.versionName;
       RunInfo runInfo=new RunInfo(name,icon,packName,versionName);
       allAppInfo.add(runInfo);
       if ((applicationInfo.flags&applicationInfo.FLAG_SYSTEM)!=0){
          allAppuser.add(runInfo);
       }else {
          allAppsystem.add(runInfo);
       }
   }
   return allAppInfo;
    }
  public static List getAppSystem(){
   getAppInfo();
   return allAppuser;
  }
   public static List getAppUser(){
    getAppInfo();
    return allAppsystem;
}
}

软件的实例(由于单个截取的,所以有一点没用的,可以不要,也可以要)

public class RunInfo {
String appname;
String packname;
 String versionName;
Drawable icon;
long usemomory;
boolean isselect;

public String getVersionName() {
    return versionName;
}

public void setVersionName(String versionName) {
    this.versionName = versionName;
}

public void isselect(boolean select){
  this.isselect=select;
}

public boolean isselect() {
    return isselect;
}

public void setIsselect(boolean isselect) {
    this.isselect = isselect;
}

public RunInfo(String appname, Drawable icon, String packname, long usemomory){
  this.appname=appname;
    this.icon=icon;
    this.packname=appname;
    this.usemomory=usemomory;
}
public RunInfo(String appname, Drawable icon, String packname, String versionName){
    this.appname=appname;
    this.icon=icon;
    this.packname=packname;
    this.versionName = versionName;

}

public String getAppname() {
    return appname;
}

public void setAppname(String appname) {
    this.appname = appname;
}

public String getPackname() {
    return packname;
}

public void setPackname(String packname) {
    this.packname = packname;
}

public Drawable getIcon() {
    return icon;
}

public void setIcon(Drawable icon) {
    this.icon = icon;
}

public String getUsemomory() {
    String size=Formatter.formatFileSize(App.appContext,usemomory);
    return size;
}

public void setUsemomory(long usemomory) {
    this.usemomory = usemomory;
}
}

软件清理的适配器

 public class SoftAdapter extends BaseAdapter {

private List datas;

public SoftAdapter(List datas) {
    this.datas = datas;
}

@Override
public int getCount() {
    return datas==null?0:datas.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.softadapter, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final RunInfo runInfo=datas.get(position);
    holder.ioce.setImageDrawable(runInfo.getIcon());
    holder.select.setChecked(runInfo.isselect());
    holder.tvName.setText(runInfo.getAppname());
    holder.tvversion.setText(runInfo.getVersionName());
    holder.packname.setText(runInfo.getPackname());
    holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            runInfo.setIsselect(isChecked);
            if (!isChecked){
                SoftcleanActivity.mCbAllselect.setChecked(false);
                return;
            }
            for (RunInfo data : datas) {
                if (!data.isselect())
                    return;
            }
            SoftcleanActivity.mCbAllselect.setChecked(true);
        }
    });
    holder.select.setChecked(runInfo.isselect());
    return holder.convertView;
}

public class ViewHolder {

    public ImageView ioce;
    public View convertView;
    public TextView tvName;
    public TextView tvversion;
    public CheckBox select;
    public TextView packname;

    public ViewHolder(View convertView) {

        this.convertView = convertView;
        select = (CheckBox) convertView.findViewById(R.id.cb_item_soft_select);
        ioce = (ImageView) convertView.findViewById(R.id.img_item_soft_ioce);
        tvName = (TextView) convertView.findViewById(R.id.tv_item__soft_name);
        tvversion = (TextView) convertView.findViewById(R.id.tv_tv_item__soft_version);
        packname = (TextView) convertView.findViewById(R.id.tv_tv_item__soft_packname);
    }
}
public  void refresh(List datas){
    this.datas.clear();
    this.datas.addAll(datas);
    notifyDataSetChanged();
}
}

你可能感兴趣的:(软件管理(显示系统所有安装软件,系统。用户,并清理))