Android obtains global process information and memory usage by process

Source: Internet
Author: User

Android obtains all process information and obtains the memory used by the process.

Package Zhangphil.process;import Java.util.list;import Android.os.bundle;import android.os.debug.memoryinfo;import Android.widget.textview;import Android.app.activity;import Android.app.activitymanager;import Android.app.activitymanager.runningappprocessinfo;import Android.content.context;public class MainActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView TV = (TextView) Findviewbyid (R.id.textview); Activitymanager Activitymanager = (activitymanager) getsystemservice (Context.activity_service); String all = "", try {list<runningappprocessinfo> List = activitymanager.getrunningappprocesses (); for (int i = 0; I < list.size (); i++) {Runningappprocessinfo p = list.get (i); String info = "First" + (i + 1) + "process \ n"; info + = "Name:" + p.processname + "\ n"; info + = "ID:" + p.pid + "\ n"; info + = "User id:" + P.uid + "\ n";//Memory used by the process, Memoryinfo Unit is kbint[] Memorypid = new int[] {P.piD}; memoryinfo[] Memoryinfo = Activitymanager.getprocessmemoryinfo (memorypid);//Process memory usage, Unit: mbint memsize = memoryInfo[0 ].dalvikprivatedirty; String count = "", if (Memsize < 1024x768) Count = memsize + "KB"; elsecount = memsize/1024 + "MB"; info + = "Memory Used (DALVIKPR Ivatedirty): "+ count +" \ n "; info + =" Important level: "+ p.importance +" \ n "; all = all + info +" \ n ";}} catch (Exception e) {e.printstacktrace ();} Tv.settext (All);}}

You need to add permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.GET_TASKS"/>

Activity_main.xml files required by Mainactivity.java:

  <scrollview  xmlns:android= "http://schemas.android.com/apk/res/android"        android:layout_width= "Match_ Parent "        android:layout_height=" match_parent ">         <textview        android:id=" @+id/textview "        Android:layout_width= "Match_parent"        android:layout_height= "match_parent"/>           </ScrollView>



Android obtains global process information and memory usage by process

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.