手机加速(获取APP的信息,并关闭)

2017-04-26  本文已影响0人  谜之龙
aa.png

查找用户的进程,系统的进程并关闭
主界面

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<RunInfo> data;
private RocketAdapter adapter;
private List<RunInfo> 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<RunInfo> 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

<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.RocketActivity">
  //顶部布局,导入一个新的布局
<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/layout_rocket_top"/>

<!-- 中间部分的布局 -->
<RelativeLayout
    android:layout_marginTop="4dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ListView
        android:id="@+id/list_rocket_appinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ProgressBar
        android:id="@+id/pb_rocket_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminateDrawable="@drawable/rotate_rocket_pb"
        android:indeterminateOnly="true"
        android:indeterminateBehavior="repeat"
        android:indeterminateDuration="600"
        android:visibility="gone"
        />

</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <CheckBox
        android:id="@+id/cb_rocket_allselct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt_rocket_state"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/bt_rocket_bg"
        android:text="只显示系统进程"
        android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>

顶部的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_rocket_model"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="手机型号"
    android:textSize="26sp"
    android:padding="4dp"
    android:layout_gravity="center_horizontal"/>

<ProgressBar
    android:id="@+id/pb_rocket_rote"
    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" />

<Button
    android:id="@+id/bt_rocket_clear"
    android:layout_width="match_parent"
    android:layout_height="46dp"
    android:text="一键清理"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="6dp"
    android:textColor="#fff"
   //自己写的点击样式
    android:background="@drawable/bt_rocket_bg"
    />
   </LinearLayout>

自己写的进度条样式ProgressBar

//图层列表
  <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 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<RunInfo> datas;

public RocketAdapter(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.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<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;
 }

获取内存信息的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是要权限,在注册清单

<!-- 杀死运行APP的权限 -->
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
上一篇下一篇

猜你喜欢

热点阅读