zoukankan      html  css  js  c++  java
  • Android项目实战_手机安全卫士进程管理


    ###1.设备进程信息获取
    获取设备运行进程

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningAppProcessInfos = am.getRunningAppProcesses();

    获取设备RAM

    MemoryInfo outInfo = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(outInfo);
    outInfo.availMem;//设备可用RAM
    outInfo.totalMem;//设备总RAM

    ###2.获取进程占用RAM
    ActivityManager am = (ActivityManager) context
    .getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{info.pid});
    long memSize = processMemoryInfo[0].getTotalPrivateDirty()*1024;

    ###3.ListView中Item中有CheckBox、Button等的焦点处理
    ListView的item布局中有CheckBox、Button等会获取焦点的控件会抢走焦点,造成ListView的item点击事件相应不了

    解决方法:控件设置

    android:clickable="false"
    android:focusable="false"

    或者

    android:focusable="false"
    //descendant 后裔; 后代; (由过去类似物发展来的) 派生物; 弟子;
    android:descendantFocusability="blocksDescendants"

    ###10.ListView中getItemAtPosition()的使用
    ListView的getItemAtPosition()方法的返回值即为adapter中getItem()方法返回的对象

    ###4.清理进程
    1.添加权限
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    2.具体实现
    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    am.killBackgroundProcesses(String pkgName);

      1 package com.hb.mobilesafe.activities;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Iterator;
      5 import java.util.List;
      6 
      7 import android.annotation.SuppressLint;
      8 import android.app.Activity;
      9 import android.app.ActivityManager;
     10 import android.app.ActivityManager.MemoryInfo;
     11 import android.graphics.Color;
     12 import android.os.Bundle;
     13 import android.text.format.Formatter;
     14 import android.view.View;
     15 import android.view.View.OnClickListener;
     16 import android.view.ViewGroup;
     17 import android.view.Window;
     18 import android.widget.AbsListView;
     19 import android.widget.AbsListView.OnScrollListener;
     20 import android.widget.AdapterView;
     21 import android.widget.AdapterView.OnItemClickListener;
     22 import android.widget.BaseAdapter;
     23 import android.widget.CheckBox;
     24 import android.widget.ImageView;
     25 import android.widget.ListView;
     26 import android.widget.RelativeLayout;
     27 import android.widget.TextView;
     28 
     29 import com.hb.demo_mobilesafe.R;
     30 import com.hb.mobilesafe.bean.ProInfo;
     31 import com.hb.mobilesafe.utils.ProgressInfoUtil;
     32 
     33 public class ProcessManagerActivity extends Activity implements OnClickListener {
     34     private TextView tv_clean,tv_allselect,tv_inverse;
     35     private ListView lv_progress_show;
     36     private TextView tv_progres_ssoft,tv_storage;
     37     private TextView tv_software_count;
     38     private List<ProInfo> infosRun;
     39     private List<ProInfo> userInfo;
     40     private List<ProInfo> sysInfo;
     41     private MyProAdapter adapter;
     42     private ActivityManager am;
     43     @Override
     44     protected void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         requestWindowFeature(Window.FEATURE_NO_TITLE);
     47         setContentView(R.layout.activity_processmanager);
     48         initView();
     49         initDate();
     50     }
     51 
     52 
     53     private void initView() {
     54         tv_clean=(TextView) findViewById(R.id.tv_clean);
     55         tv_allselect=(TextView) findViewById(R.id.tv_allselect);
     56         tv_inverse=(TextView) findViewById(R.id.tv_inverse);
     57 
     58         tv_progres_ssoft=(TextView) findViewById(R.id.tv_progres_ssoft);
     59         tv_storage=(TextView) findViewById(R.id.tv_storage);
     60         tv_software_count=(TextView) findViewById(R.id.tv_software_count);
     61 
     62         lv_progress_show=(ListView) findViewById(R.id.lv_progress_show);
     63 
     64         tv_clean.setOnClickListener(this);
     65         tv_allselect.setOnClickListener(this);
     66         tv_inverse.setOnClickListener(this);
     67     }
     68     private void initDate() {
     69 
     70         tv_progres_ssoft.setText("运行中的进程:"+getRunpro()+"个");
     71         tv_storage.setText("可用内存/总内存:"+Formatter.formatFileSize(this, getAvimen())
     72                 +"/"+Formatter.formatFileSize(this, getTotalMen()));
     73         adapter = new MyProAdapter();
     74         sysInfo = new ArrayList<ProInfo>();
     75         userInfo = new ArrayList<ProInfo>();
     76         new Thread(){
     77             public void run() {
     78                 infosRun = ProgressInfoUtil.getInfosRun(ProcessManagerActivity.this);
     79                 isSysUserApp();
     80                 runOnUiThread(new Runnable() {
     81                     public void run() {
     82                         lv_progress_show.setAdapter(adapter);
     83                     }
     84                 });
     85             };
     86         }.start();
     87         lv_progress_show.setOnScrollListener(new OnScrollListener() {
     88 
     89             @Override
     90             public void onScrollStateChanged(AbsListView view, int scrollState) {
     91 
     92             }
     93 
     94             @Override
     95             public void onScroll(AbsListView view, int firstVisibleItem,
     96                     int visibleItemCount, int totalItemCount) {
     97                 if(userInfo!=null && sysInfo !=null){
     98 
     99                     if(firstVisibleItem==0){
    100                         tv_software_count.setVisibility(View.GONE);
    101                     }else{
    102 
    103                         tv_software_count.setVisibility(View.VISIBLE);
    104                     }
    105                     if(firstVisibleItem <userInfo.size()){
    106                         tv_software_count.setText("用户进程:"+userInfo.size());
    107                     }else{
    108                         tv_software_count.setText("系统进程:"+sysInfo.size());
    109                     }
    110                 }
    111             }
    112         });
    113         lv_progress_show.setOnItemClickListener(new OnItemClickListener() {
    114 
    115             @Override
    116             public void onItemClick(AdapterView<?> parent, View view,
    117                     int position, long id) {
    118                 if(position == 0 || position == userInfo.size()+1){
    119                     return;
    120                 }else if(position < userInfo.size()+1){
    121 
    122                     int newPosition=position -1;
    123                     boolean isCheck = userInfo.get(newPosition).isCheck();
    124                     if(isCheck){
    125 
    126                         userInfo.get(newPosition).setCheck(false);
    127                     }else{
    128 
    129                         userInfo.get(newPosition).setCheck(true);
    130                     }
    131                 }else{
    132                     int newPosition=position-userInfo.size()-2;
    133                     boolean isCheck = sysInfo.get(newPosition).isCheck();
    134                     if(isCheck){
    135                         sysInfo.get(newPosition).setCheck(false);
    136                     }else{
    137 
    138                         sysInfo.get(newPosition).setCheck(true);
    139                     }
    140                 }
    141                 adapter.notifyDataSetChanged();
    142             }
    143         });
    144     }
    145 
    146     /**
    147      * 一键清理,全选,反选
    148      */
    149     @Override
    150     public void onClick(View v) {
    151         switch (v.getId()) {
    152         case R.id.tv_clean:
    153             am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    154             Iterator<ProInfo> userIterator = userInfo.iterator();
    155             Iterator<ProInfo> sysIterator = sysInfo.iterator();
    156             while (userIterator.hasNext()) {
    157                 ProInfo next = userIterator.next();
    158                 if(next.isCheck()){
    159                     am.killBackgroundProcesses(next.getAppPackageName());
    160                     userIterator.remove();
    161                 }
    162             }
    163             while (sysIterator.hasNext()) {
    164                 ProInfo next = sysIterator.next();
    165                 if(next.isCheck()){
    166                     am.killBackgroundProcesses(next.getAppPackageName());
    167                     sysIterator.remove();
    168                 }
    169             }
    170             adapter.notifyDataSetChanged();
    171 
    172             break;
    173 
    174         case R.id.tv_allselect:
    175             for (ProInfo infos : infosRun) {
    176                 if(!"com.hb.demo_mobilesafe".equals(infos.getAppPackageName())){
    177                     infos.setCheck(true);
    178                 }
    179             }
    180             adapter.notifyDataSetChanged();
    181             break;
    182 
    183         case R.id.tv_inverse:
    184 
    185             for (ProInfo infos : infosRun) {
    186                 if(infos.isCheck() && !("com.hb.demo_mobilesafe").equals(infos.getAppPackageName())){
    187                     infos.setCheck(false);
    188                 }else if(!infos.isCheck() && !("com.hb.demo_mobilesafe").equals(infos.getAppPackageName())){
    189                     infos.setCheck(true);
    190                 }
    191 
    192             }
    193             adapter.notifyDataSetChanged();
    194             break;
    195         }
    196     }
    197     /**
    198      * 运行中的进程
    199      * @return
    200      */
    201     private  int getRunpro(){
    202         ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
    203         int size = am.getRunningAppProcesses().size();
    204         return size;
    205 
    206     }
    207     /**
    208      * 可用内存
    209      */
    210     private  long getAvimen(){
    211         ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
    212         MemoryInfo outInfo = new MemoryInfo();
    213         am.getMemoryInfo(outInfo);
    214         return outInfo.availMem;
    215 
    216     }
    217     /**
    218      * 总内存
    219      */
    220     private  long getTotalMen(){
    221         ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
    222         MemoryInfo outInfo = new MemoryInfo();
    223         am.getMemoryInfo(outInfo);
    224         return outInfo.totalMem;
    225 
    226     }
    227     /**
    228      * 区分系统应用和用户应用
    229      */
    230     public void isSysUserApp(){
    231 
    232         for (ProInfo proInfo: infosRun) {
    233             boolean sys = proInfo.isSys();
    234             if(sys){
    235                 sysInfo.add(proInfo);
    236             }else{
    237                 userInfo.add(proInfo);
    238 
    239             }
    240         }
    241     }
    242     class MyProAdapter extends BaseAdapter{
    243         @SuppressWarnings("null")
    244         @SuppressLint("ViewHolder") @Override
    245         public View getView(int position, View view, ViewGroup parent) {
    246             ViewHolder holder = null;
    247             if(view != null && view instanceof RelativeLayout){
    248                 holder=(ViewHolder) view.getTag();
    249             }else{
    250                 holder=new ViewHolder();
    251                 view = View.inflate(ProcessManagerActivity.this, R.layout.proinfo_item, null);
    252                 holder.iv_appIcon=(ImageView) view.findViewById(R.id.iv_pro_icon);
    253                 holder.tv_appName=(TextView) view.findViewById(R.id.tv_pro_name);
    254                 holder.tv_proSize=(TextView) view.findViewById(R.id.tv_pro_size);
    255                 holder.cb_isCheck=(CheckBox) view.findViewById(R.id.cb_ischeak);
    256                 view.setTag(holder);
    257             }
    258             if(position == 0){
    259                 TextView tv0= new TextView(ProcessManagerActivity.this);
    260                 if(userInfo.size()==0){
    261                     tv0.setHeight(0);
    262                     return tv0;
    263                 }
    264                 tv0.setClickable(false);
    265                 tv0.setTextColor(Color.WHITE);
    266                 tv0.setBackgroundColor(Color.GRAY);
    267                 tv0.setText("用户进程:"+userInfo.size());
    268                 return tv0;
    269             }else if(position ==userInfo.size()+1){
    270                 TextView tv1= new TextView(ProcessManagerActivity.this);
    271                 tv1.setClickable(false);
    272                 tv1.setTextColor(Color.WHITE);
    273                 tv1.setBackgroundColor(Color.GRAY);
    274                 tv1.setText("系统进程:"+sysInfo.size());
    275                 return tv1;
    276             }else if(position <userInfo.size()+1){
    277                 int newPosition = position-1;
    278                 ProInfo proInfo = userInfo.get(newPosition);
    279                 holder.iv_appIcon.setImageDrawable(proInfo.getAppIcon());
    280                 holder.tv_appName.setText(proInfo.getAppName());
    281                 holder.tv_proSize.setText("进程大小:"+Formatter.formatFileSize(ProcessManagerActivity.this, proInfo.getProSize()));
    282                 holder.cb_isCheck.setChecked(proInfo.isCheck());
    283                 String appPackageName = proInfo.getAppPackageName();
    284                 if(appPackageName.equals(getPackageName())){
    285                     holder.cb_isCheck.setVisibility(View.GONE);
    286                 }else {
    287                     holder.cb_isCheck.setVisibility(View.VISIBLE);
    288                 }
    289 
    290             }else{
    291                 int newPosition = position-userInfo.size()-2;
    292                 ProInfo proInfo = sysInfo.get(newPosition);
    293                 holder.iv_appIcon.setImageDrawable(proInfo.getAppIcon());
    294                 holder.tv_appName.setText(proInfo.getAppName());
    295                 holder.tv_proSize.setText("进程大小:"+Formatter.formatFileSize(ProcessManagerActivity.this, proInfo.getProSize()));
    296                 holder.cb_isCheck.setChecked(proInfo.isCheck());
    297             }
    298             return view;
    299         }
    300         @Override
    301         public int getCount() {
    302             return userInfo.size()+1+sysInfo.size()+1;
    303         }
    304 
    305         @Override
    306         public Object getItem(int position) {
    307             return null;
    308         }
    309 
    310         @Override
    311         public long getItemId(int position) {
    312             return 0;
    313         }
    314     }
    315     class ViewHolder{
    316         ImageView iv_appIcon;
    317         CheckBox cb_isCheck;
    318         TextView tv_appName;
    319         TextView tv_proSize;
    320     }
    321 }

    xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:gravity="center_horizontal" >
     6 
     7     <TextView
     8         android:id="@+id/tv_title"
     9         android:layout_width="match_parent"
    10         android:layout_height="46dp"
    11         android:background="#A1FF80"
    12         android:gravity="center_vertical"
    13         android:text="进程管理"
    14         android:textSize="20sp" />
    15 
    16     <RelativeLayout
    17         android:id="@+id/rl_size"
    18         android:layout_width="match_parent"
    19         android:layout_height="wrap_content"
    20         android:layout_below="@id/tv_title" >
    21 
    22         <TextView
    23             android:id="@+id/tv_progres_ssoft"
    24             android:layout_width="wrap_content"
    25             android:layout_height="wrap_content"
    26             android:text="运行中的进程:19个"
    27             android:textSize="10sp" />
    28 
    29         <TextView
    30             android:id="@+id/tv_storage"
    31             android:layout_width="wrap_content"
    32             android:layout_height="wrap_content"
    33             android:layout_alignParentRight="true"
    34             android:text="可用内存/总内存:20MB/60MB"
    35             android:textSize="10sp" />
    36     </RelativeLayout>
    37 
    38     <TextView
    39         android:id="@+id/tv_software_count"
    40         android:layout_width="match_parent"
    41         android:layout_height="wrap_content"
    42         android:layout_below="@id/rl_size"
    43         android:background="#888888"
    44         android:visibility="gone"
    45         android:text="用户进程:5"
    46         android:textColor="#ffffff"
    47         />
    48 
    49     <ListView
    50         android:clickable="true"
    51         android:id="@+id/lv_progress_show"
    52         android:layout_width="match_parent"
    53         android:layout_height="wrap_content"
    54         android:layout_above="@+id/ll"
    55         android:layout_below="@id/tv_software_count" />
    56 
    57     <LinearLayout
    58         android:id="@+id/ll"
    59         android:layout_width="match_parent"
    60         android:layout_height="wrap_content"
    61         android:layout_alignParentBottom="true"
    62         android:orientation="horizontal"
    63         android:padding="2dip" >
    64 
    65         <TextView
    66             android:id="@+id/tv_clean"
    67             android:layout_width="0dp"
    68             android:layout_height="wrap_content"
    69             android:layout_weight="2"
    70             android:background="@drawable/clean_button_selector"
    71             android:textSize="15sp" />
    72 
    73         <TextView
    74             android:id="@+id/tv_allselect"
    75             android:layout_width="0dp"
    76             android:layout_height="wrap_content"
    77             android:layout_weight="1"
    78             android:background="@drawable/select_btn_selector"
    79             android:textSize="15sp" />
    80 
    81         <TextView
    82             android:id="@+id/tv_inverse"
    83             android:layout_width="0dp"
    84             android:layout_height="wrap_content"
    85             android:layout_weight="1"
    86             android:background="@drawable/inverse_btn_selector"
    87             android:textSize="15sp" />
    88     </LinearLayout>
    89 
    90 </RelativeLayout>
    View Code
  • 相关阅读:
    每天一点点java---继承exception类来实现自己的异常类
    由Java代码运行shell命令/脚本
    虚拟现实及增强现实(眼镜或头盔)现状与未来-基本介绍
    Struts2大约Action系统培训6大约action的接受三个参数的方法
    main真正的形状函数
    为了交易-存储过程
    Oracle 数据库 JOB 失败后解密法重试
    [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
    十依据一个有用的算法来找到最小(最大)的k的数量-线性搜索算法
    Maven配置
  • 原文地址:https://www.cnblogs.com/boket/p/6991795.html
Copyright © 2011-2022 走看看