写基类的东西
软件管理的主界面
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);
}
}
主界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.gfd.phone.activity.SoftmgrActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<com.gfd.phone.view.CleaeArcView
android:id="@+id/myview_softmgr_root"
android:layout_width="140dp"
android:layout_height="140dp" />
<com.gfd.phone.view.CleaeArcView
android:id="@+id/myview_softmgr_sd"
android:layout_width="140dp"
android:layout_height="140dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/myview_softmgr_root"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#3852d2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机的内存"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/myview_softmgr_in"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#e7cf30"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="外部储存卡"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机内置空间"
android:textSize="15dp"/>
<ProgressBar
android:id="@+id/pb_softmgr_root"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="6dp"
android:max="100"
android:progressDrawable="@drawable/progress_bg" />
<TextView
android:id="@+id/tv_soft_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已用内存:345M/23G"
android:textSize="16sp"
android:layout_marginRight="20dp"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机外置空间"
android:textSize="15dp"/>
<ProgressBar
android:id="@+id/pb_softmgr_sd"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="6dp"
android:max="100"
android:progressDrawable="@drawable/progress_bg" />
<TextView
android:id="@+id/tv_soft_sd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已用内存:345M/23G"
android:textSize="16sp"
android:layout_marginRight="20dp"
android:layout_gravity="right" />
<TextView
android:id="@+id/tv_soft_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="所有软件"
android:paddingRight="10dp"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:clickable="true"
android:paddingLeft="10dp"
android:drawableRight="@drawable/image_bg_pre"/>
<TextView
android:id="@+id/tv_soft_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户软件"
android:paddingRight="10dp"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:clickable="true"
android:paddingLeft="10dp"
android:drawableRight="@drawable/image_bg_pre"/>
<TextView
android:id="@+id/tv_soft_system"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="系统软件"
android:paddingRight="10dp"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:clickable="true"
android:paddingLeft="10dp"
android:drawableRight="@drawable/image_bg_pre"/>
</LinearLayout>
布局中扇形的布局,及自定义控件(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();
}
}
主界面布局的进度条,在很多情况下是可以复用的
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 水平进度条背景的样式 -->
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#b3b1b1"/>
</shape>
</item>
<!-- 水平进度条表示进度的样式 -->
<item android:id="@android:id/progress">
<clip><!-- 剪裁-->
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#17d4f5"/>
</shape>
</clip>
</item>
</layer-list>
软件清理的界面
public class SoftcleanActivity extends BaseActivity {
public SoftAdapter softAdapter;
public ListView mList;
public List<RunInfo> datas=new ArrayList<>();
public List<RunInfo> 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);
}
}
}
清理界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.gfd.phone.activity.SoftcleanActivity">
<ListView
android:id="@+id/list_soft"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_soft_seclect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
<Button
android:id="@+id/bt_item_soft_clean"
style=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="卸载软件"
android:textSize="20dp"
android:background="@drawable/bt_rocket_bg"
android:onClick="bt_soft_all"/>
</LinearLayout>
</LinearLayout>
获取各种软件的代码
public class Program {
public static List<RunInfo> runInfoList;
public static List<RunInfo> allUser;
public static List<RunInfo> user;
private static PackageInfo packageInfo;
private static List<RunInfo> allAppInfo;
private static List<RunInfo> allAppuser;
private static List<RunInfo> allAppsystem;
public static List<RunInfo> getAllinfo(){
ActivityManager am= (ActivityManager) App.appContext.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager pm= App.appContext.getPackageManager();
List<ActivityManager.RunningAppProcessInfo> 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<RunInfo> getAllUser(){
getAllinfo();
return allUser;
}
public static List<RunInfo> 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<RunInfo> getAppInfo(){
allAppInfo = new ArrayList<>();
allAppuser = new ArrayList<>();
allAppsystem = new ArrayList<>();
PackageManager pm=App.appContext.getPackageManager();
List<ApplicationInfo> 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<RunInfo> getAppSystem(){
getAppInfo();
return allAppuser;
}
public static List<RunInfo> 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<RunInfo> datas;
public SoftAdapter(List<RunInfo> 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<RunInfo> datas){
this.datas.clear();
this.datas.addAll(datas);
notifyDataSetChanged();
}
}