查找用户的进程,系统的进程并关闭
主界面
public class RocketActivity extends BaseActivity implements View.OnClickListener {
private TextView mTvModel;
private ProgressBar mProgressMemory;
private Button mBtnClear;
private ListView mListApps;
private ProgressBar mProgress;
private Button mBtnState;
private List data;
private RocketAdapter adapter;
private List allUser;
public static CheckBox mCbAllSelect;
@Override
protected boolean isShowActionBar() {
return true;
}
@Override
protected void initData() {
setTitle("手机加速");
mTvModel.setText(Build.MODEL);
long avaiable = MemoryInfoManager.getPhoneAvailableMemoeySize();
long total = MemoryInfoManager.getPhoneTotalMemoeySize();
double rote = (total - avaiable) * 1.0 / total;
mProgressMemory.setProgress((int) (rote * 100));
mBtnState.setText("只显示系统进程");
mBtnState.setTag(true);
new Thread() {
@Override
public void run() {
allUser = Program.getUser();
RocketActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new RocketAdapter(allUser);
mListApps.setAdapter(adapter);
}
});
}
}.start();
}
@Override
protected void initView() {
mTvModel = getViewById(R.id.tv_rocket_model);
mProgressMemory = getViewById(R.id.pb_rocket_rote);
mBtnClear = getViewById(R.id.bt_rocket_clear);
mListApps = getViewById(R.id.list_rocket_appinfo);
mProgress = getViewById(R.id.pb_rocket_clear);
mBtnState = getViewById(R.id.bt_rocket_state);
mCbAllSelect = getViewById(R.id.cb_rocket_allselct);
}
@Override
protected void setListaner() {
mBtnClear.setOnClickListener(this);
mBtnState.setOnClickListener(this);
//全选选择监听
mCbAllSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (RunInfo runInfo : allUser) {
runInfo.setIsselect(mCbAllSelect.isChecked());
}
adapter.notifyDataSetChanged();
}
});
}
@Override
protected int getLayoutId() {
return R.layout.activity_rocket;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_rocket_clear://一键清理
List runInfoList=new ArrayList<>();
for (RunInfo info : allUser) {
if (info.isselect()){
Program.killApp(info.getPackname());
}else{
runInfoList.add(info);
}
}
allUser.clear();
allUser.addAll(runInfoList);
adapter.notifyDataSetChanged();
break;
case R.id.bt_rocket_state://进程切换
final boolean isUserApp = (boolean) mBtnState.getTag();
new Thread() {
@Override
public void run() {
if (isUserApp) {//显示当前用户信息
mBtnState.setTag(false);
data = Program.getAllUser();
Log.e("aa", "size" + data.size());
} else {//显示系统信息
mBtnState.setTag(true);
data = Program.getUser();
}
//只能在主线程更新UI布局
RocketActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
boolean tag = (boolean) mBtnState.getTag();
String msg = (tag == true) ? "只显示用户进程" : "只显示系统进程";
mBtnState.setText(msg);
allUser.clear();
allUser.addAll(data);
adapter.notifyDataSetChanged();
}
});
}
}.start();
break;
}
}
}
主界面的布局XML
//顶部布局,导入一个新的布局
顶部的布局
自己写的进度条样式ProgressBar
//图层列表
-
-
实例工具
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 RocketAdapter extends BaseAdapter {
private List datas;
public RocketAdapter(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.rocket_list, 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.tvsize.setText(runInfo.getUsemomory());
holder.packname.setText(runInfo.getPackname());
//这个是全选按钮的设置
holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
runInfo.setIsselect(isChecked);
if (!isChecked){
RocketActivity.mCbAllSelect.setChecked(false);
return;
}
for (RunInfo data : datas) {
if (!data.isselect())
return;
}
RocketActivity.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 tvsize;
public CheckBox select;
public TextView packname;
public ViewHolder(View convertView) {
this.convertView = convertView;
select = (CheckBox) convertView.findViewById(R.id.cb_item_rocket_select);
ioce = (ImageView) convertView.findViewById(R.id.img_item_rocket_ioce);
tvName = (TextView) convertView.findViewById(R.id.tv_item__rocket_name);
tvsize = (TextView) convertView.findViewById(R.id.tv_tv_item__rocket_size);
packname = (TextView) convertView.findViewById(R.id.tv_tv_item__rocket_packname);
}
}
}
获得各种APP信息的class
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;
}
获取内存信息的Class
public class MemoryInfoManager {
/**
* 获取手机可用内存
* @return bit
*/
public static long getPhoneAvailableMemoeySize(){
ActivityManager am = (ActivityManager) App.appContext.getSystemService(Context.ACTIVITY_SERVICE);
//获取内存信息
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;//bit
}
/**
* 获取手机总的内存大小
* @return bit
*/
public static long getPhoneTotalMemoeySize(){
ActivityManager am = (ActivityManager) App.appContext.getSystemService(Context.ACTIVITY_SERVICE);
//获取内存信息
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.totalMem;//bit
}
关闭App是要权限,在注册清单