Android gets the running program and kill it.

Source: Internet
Author: User

Android gets the running program and kill it.

Obtain the running program and add it to the adapter class of a listview. The method is as follows:

// The public List that is running
 
  
GetRunningProcess () {pi = new PackagesInfo (this); am = (ActivityManager) getSystemService (ACTIVITY_SERVICE); // get the running application run = am. getRunningAppProcesses (); // obtain the Package Manager. Here, the package name is used to obtain the program icon and program name pm = this. getPackageManager (); List
  
   
List = new ArrayList
   
    
(); For (RunningAppProcessInfo ra: run) {// here it mainly filters out system applications and phone applications. Of course, you can also comment it out. If (ra. processName. equals ("system") | ra. processName. equals ("com. android. phone ") {continue;} if (pi. getInfo (ra. processName) = null) {continue;} int [] myMempid = new int [] {ra. pid}; Programe pr = new Programe (); String xx = "" + ra. processName; Log. d ("zphlog", "ra. processName = "+ xx); pr. setIcon (pi. getInfo (ra. processName ). loadIcon (pm); pr. setName (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); System. out. println (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); // PIDpr. setPID ("PID:" + ra. pid); // memoryDebug. memoryInfo [] memoryInfo = am. getProcessMemoryInfo (myMempid); double memSize = memoryInfo [0]. dalvikPrivateDirty/1024.0; int temp = (int) (memSize * 100); memSize = temp/100.0; pr. setMemory ("Memory:" + memSize); list. add (pr);} return list ;}
   
  
 

The following describes how to obtain the remaining running memory of a running program on the mobile phone:

// Update available memory information public void updatemandpakinfo () {// get MemoryInfo object ActivityManager. memoryInfo memoryInfo = new ActivityManager. memoryInfo (); // obtain the available memory of the system, which is saved on the MemoryInfo object am. getMemoryInfo (memoryInfo); long memSize = memoryInfo. availMem; // character type conversion String restMemSize = Formatter. formatFileSize (getBaseContext (), memSize); mRestMemory. setText ("available space:" + restMemSize); // mPakcgeNums. setText (run. size (); mPakcgeNums. setText ("running:" + run. size () + "count ");}

Take a look


The detailed code is as follows:

ActivityMain. java

Public class ActivityMain extends Activity {private static final int MSG_REFRESH = 555; private static final int MSG_KILL = 444; private ListView mListView; private Button mKillBtn; private Button mRefreshBtn; private TextView mPakcgeNums; private TextView mRestMemory; private PackagesInfo pi; private ActivityManager am; private List
 
  
Run; private PackageManager pm; private long mExitTime; private static final int EXIT = 0x113; private static final int ABOUT = 0x114; private static final int INFO = 0x115; private static final int SHARE = 0x116; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_INDETERMINATE_PROGRESS); this. requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); mListView = (ListView) findViewById (R. id. list); mKillBtn = (Button) findViewById (R. id. kill_all); mRefreshBtn = (Button) findViewById (R. id. refresh_run); mPakcgeNums = (TextView) findViewById (R. id. packagenums); mRestMemory = (TextView) findViewById (R. id. restmemory); List
  
   
List = getRunningProcess (); ListAdapter adapter = new ListAdapter (list, this); mListView. setAdapter (adapter); upDateMemAndPakInfo (); // Display Memory information // getListView (). setAdapter (adapter); mKillBtn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubhandler. sendEmptyMessage (MSG_KILL) ;}}); mRefreshBtn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubnew Thread () {public void run () {handler. sendEmptyMessage (MSG_REFRESH );}}. start () ;}}) ;}// a public List that is running
   
    
GetRunningProcess () {pi = new PackagesInfo (this); am = (ActivityManager) getSystemService (ACTIVITY_SERVICE); // get the running application run = am. getRunningAppProcesses (); // obtain the Package Manager. Here, the package name is used to obtain the program icon and program name pm = this. getPackageManager (); List
    
     
List = new ArrayList
     
      
(); For (RunningAppProcessInfo ra: run) {// here it mainly filters out system applications and phone applications. Of course, you can also comment it out. If (ra. processName. equals ("system") | ra. processName. equals ("com. android. phone ") {continue;} if (pi. getInfo (ra. processName) = null) {continue;} int [] myMempid = new int [] {ra. pid}; Programe pr = new Programe (); String xx = "" + ra. processName; Log. d ("zphlog", "ra. processName = "+ xx); pr. setIcon (pi. getInfo (ra. processName ). loadIcon (pm); pr. setName (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); System. out. println (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); // PIDpr. setPID ("PID:" + ra. pid); // memoryDebug. memoryInfo [] memoryInfo = am. getProcessMemoryInfo (myMempid); double memSize = memoryInfo [0]. dalvikPrivateDirty/1024.0; int temp = (int) (memSize * 100); memSize = temp/100.0; pr. setMemory ("Memory:" + memSize); list. add (pr) ;}return list;} Handler handler = new Handler () {public void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case MSG_REFRESH: List
      
        List = getRunningProcess (); ListAdapter adapter = new ListAdapter (list, ActivityMain. this); mListView. setAdapter (adapter); upDateMemAndPakInfo (); Toast. makeText (ActivityMain. this, "Refresh successful", Toast. LENGTH_SHORT ). show (); break; case MSG_KILL: for (RunningAppProcessInfo ra: run) {// here it mainly filters out system applications and phone applications. Of course, you can also comment it out. If (ra. processName. equals ("system") | ra. processName. equals ("com. android. phone ") {continue;} if (pi. getInfo (ra. processName) = null) {continue;} am. killBackgroundProcesses (ra. processName); // Log. d ("zphlog", "pid =" + ra. pid);} Toast. makeText (ActivityMain. this, "kill succeeded", Toast. LENGTH_SHORT ). show (); break ;}}; // update available memory information public void updatemandpakinfo () {// obtain the MemoryInfo object ActivityManager. memoryInfo memoryInfo = new ActivityManager. memoryInfo (); // obtain the available memory of the system, which is saved on the MemoryInfo object am. getMemoryInfo (memoryInfo); long memSize = memoryInfo. availMem; // character type conversion String restMemSize = Formatter. formatFileSize (getBaseContext (), memSize); mRestMemory. setText ("available space:" + restMemSize); // mPakcgeNums. setText (run. size (); mPakcgeNums. setText ("running:" + run. size () + "");} // click the return key of the mobile phone to return to the public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {if (System. currentTimeMillis ()-mExitTime)> 2000) {Toast. makeText (this, "exit the program again", Toast. LENGTH_SHORT ). show (); mExitTime = System. currentTimeMillis () ;}else {finish () ;}return true ;}return super. onKeyDown (keyCode, event) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {menu. add (0, ABOUT, 0, "ABOUT application"); menu. add (0, INFO, 0, "system information"); menu. add (0, SHARE, 0, "SHARE application"); menu. add (0, EXIT, 0, "EXIT application"); return super. onCreateOptionsMenu (menu) ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem mi) {switch (mi. getItemId () {case ABOUT: about_info (); break; case INFO: system_info (); break; case SHARE: share (); break; case EXIT: finish (); break;} return true;} public void about_info () {Intent intent = new Intent (this, AboutTaskKiller. class); startActivity (intent);} public void system_info () {Intent intent = new Intent (this, SystemInfo. class); startActivity (intent);} public void share () {Intent intent = new Intent (Intent. ACTION_SEND); intent. setType ("text/plain"); // plain text intent. putExtra (Intent. EXTRA_SUBJECT, "share"); intent. putExtra (Intent. EXTRA_TEXT, "I found a useful TaskKiller"); startActivity (intent );}}
      
     
    
   
  
 
ListAdapter. java

Public class ListAdapter extends BaseAdapter {List
 
  
List = new ArrayList
  
   
(); LayoutInflater la; Context context; public ListAdapter (List
   
    
List, Context context) {this. list = list; this. context = context;} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn list. size () ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn list. get (position) ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {la = LayoutInflater. from (context); convertView = la. inflate (R. layout. list_item, null); holder = new ViewHolder (); holder. imgage = (ImageView) convertView. findViewById (R. id. image); holder. text = (TextView) convertView. findViewById (R. id. text); holder. PID = (TextView) convertView. findViewById (R. id. pid); holder. memory = (TextView) convertView. findViewById (R. id. memory); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} final Programe pr = (Programe) list. get (position); // set the icon holder. imgage. setImageDrawable (pr. getIcon (); // set the program name holder. text. setText (pr. getName (); // set PIDholder. PID. setText (pr. getPID (); // set memoryholder. memory. setText (pr. getMemory (); return convertView;} class ViewHolder {TextView text; ImageView imgage; TextView memory; TextView PID ;}
   
  
 

Programe. java

Public class Programe {// icon private Drawable icon; // program name private String name; private String pID; private String memory; public Drawable getIcon () {return icon ;} public void setIcon (Drawable icon) {this. icon = icon;} public String getName () {return name;} public void setName (String name) {this. name = name;} public void setPID (String id) {this. pID = id;} public String getPID () {return pID;} public void setMemory (String m) {this. memory = m;} public String getMemory () {return memory ;}}

Activity_main.xml

     
          
           
       
      
          
           
       
      
  
 

List_item.xml

 
     
      
          
           
           
       
  
 

The above are the main implementation code. Due to the length, I will not continue to paste the code here. I will summarize my ideas.

First, I first talked about getting information about a running program.

You can use the following method to obtain

// Running
Public List GetRunningProcess (){
Pi = new PackagesInfo (this );


Am = (ActivityManager) getSystemService (ACTIVITY_SERVICE );
// Obtain the running application
Run = am. getRunningAppProcesses ();
// Obtain the Package Manager. The package name is used to obtain the program icon and program name.
Pm = this. getPackageManager ();
List List = new ArrayList ();


For (RunningAppProcessInfo ra: run ){
// Here, we mainly filter out system applications and phone applications. Of course, you can also comment it out.
If (ra. processName. equals ("system ")
| Ra. processName. equals ("com. android. phone ")){
Continue;
}
If (pi. getInfo (ra. processName) = null ){
Continue;
}
Int [] myMempid = new int [] {ra. pid };
Programe pr = new Programe ();
String xx = "" + ra. processName;
Log. d ("zphlog", "ra. processName =" + xx );
Pr. setIcon (pi. getInfo (ra. processName). loadIcon (pm ));
Pr. setName (pi. getInfo (ra. processName). loadLabel (pm). toString ());


System. out. println (pi. getInfo (ra. processName). loadLabel (pm)
. ToString ());
// PID
Pr. setPID ("PID:" + ra. pid );
// Memory
Debug. MemoryInfo [] memoryInfo = am. getProcessMemoryInfo (myMempid );
Double memSize = memoryInfo [0]. dalvikPrivateDirty/1024.0;
Int temp = (int) (memSize * 100 );
MemSize = temp/100.0;
Pr. setMemory ("Memory:" + memSize );


List. add (pr );
}
Return list;
}

Then, the information is saved in an Adapter of the listview.

The next step is to kill them. I can see three methods on the Internet to kill the program. The method I use here is

For (RunningAppProcessInfo ra: run) {// here, it mainly filters out system applications and phone applications. Of course, you can also comment it out. If (ra. processName. equals ("system") | ra. processName. equals ("com. android. phone ") {continue;} if (pi. getInfo (ra. processName) = null) {continue;} am. killBackgroundProcesses (ra. processName); // Log. d ("zphlog", "pid =" + ra. pid );}
Of course, it is carried out in the for loop, followed by some other things, such as scanning a running program at startup takes some time, we can let the program read an image when it is started, and wait for two or three seconds to get the running program list information loading time.

I used an Activity named StartUI. java.

public class StartUI extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.welcome_activity);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Intent intent = new Intent(StartUI.this, ActivityMain.class);startActivity(intent);StartUI.this.finish();}}, 2500);}}
Its Layout file is actually a simple ImageView.

The following is a simple Demo I wrote, source code

http://download.csdn.net/detail/u010443618/7829959
Prepared Apk

http://download.csdn.net/detail/u010443618/7829977
If you have any questions or suggestions, I would like to share them with you. 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.