Android Cleanup Device Memory detailed complete example (b)

Source: Internet
Author: User

Mainactivity as follows:

Package Cc.c;import Java.io.bufferedreader;import Java.io.filereader;import java.io.ioexception;import Java.util.list;import Android.app.activity;import Android.app.activitymanager;import Android.app.activitymanager.memoryinfo;import Android.app.activitymanager.runningappprocessinfo;import Android.content.context;import Android.os.bundle;import Android.view.view;import Android.widget.Button;import android.widget.textview;/** * Demo Description: * Clean phone memory * * Reference: * 1 http://blog.30c.org/1816.html * 2 Http://www.cnblogs.com/hel loandroid/archive/2011/10/14/2212334.html * Thank you very much * * Note permissions: * <uses-permission android:name= "ANDROID.P Ermission. Kill_background_processes "/> * */public class Mainactivity extends Activity {private TextView mtotalmemorytextview;p Rivate TextView mavailmemorytextview;private Button mcleanbutton;private TextView mcleaninfotextview;private Activitymanager mactivitymanager;private stringbuffer mcleaninfostringbuffer;private long AvailMemory;private longTotalmemory;private list<runningappprocessinfo> mrunningappprocessinfolist; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {mcleaninfostringbuffer = new StringBuffer (); mactivitymanager= (Activitymanager) Getsystemservice ( Context.activity_service); Mtotalmemorytextview = (TextView) Findviewbyid (R.id.totalmemorytextview); Mavailmemorytextview = (TextView) Findviewbyid (r.id.availmemorytextview); Mcleaninfotextview = (TextView) Findviewbyid (r.id.cleaninfotextview); Mcleanbutton = (Button) Findviewbyid (R.id.cleanbutton); totalmemory = Gettotalmemory (); availmemory = Getavailmemory (); Mtotalmemorytextview.settext (totalmemory + "MB"); Mavailmemorytextview.settext (availmemory + "MB"), Mcleanbutton.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {runningappprocessinfo runningappprocessinfo=null;mrunningappprocessinfolist= Mactivitymanager.getrunningappprocessES ();//list<activitymanager.runningserviceinfo> Serviceinfos = mactivitymanager.getrunningservices (+); Mrunningappprocessinfolist! = null) {for (int i = 0; i < mrunningappprocessinfolist.size (); ++i) {Runningappprocessinfo = Mrunningappprocessinfolist.get (i);//a process with a general value greater than runningappprocessinfo.importance_service//is an unused process for a long time or an empty process/ A process with a general value greater than runningappprocessinfo.importance_visible//is a non-visible process that runs if in the background (Runningappprocessinfo.importance > runningappprocessinfo.importance_visible) {string[] pkgList = runningappprocessinfo.pkglist;for (int j = 0; J < PkgLis T.length; ++J) {mactivitymanager.killbackgroundprocesses (pkglist[j]); Mcleaninfostringbuffer.append (PkgList[j] + "is killed                ... \ n "); Mcleaninfotextview.settext (mcleaninfostringbuffer.tostring ());}}} Get the remaining memory again to calculate the scavenging value mcleaninfostringbuffer.append ("Total cleanup:" + (Getavailmemory ()-availmemory) + "MB"); Mcleaninfotextview.settext (Mcleaninfostringbuffer.tostring ()); Mavailmemorytextview.settext (getAvailMemory () + " MB ");}});} Private Long Gettotalmemory () {//System memory information file string FilePath = "/proc/meminfo"; String lineString; string[] Stringarray;long totalmemory = 0;try {FileReader FileReader = new FileReader (FilePath); BufferedReader BufferedReader = new BufferedReader (filereader,1024 * 8);//Read Meminfo first line, get total system memory size linestring = Bufferedreader.readline ();//Split Stringarray = Linestring.split ("\\s+") by space;//Get Total system memory, Unit Kbtotalmemory = Integer.valueof (Stringarray[1]). Intvalue (); Bufferedreader.close (); SYSTEM.OUT.PRINTLN ("------> linestring=" + linestring+ ", stringarray[0]=" + stringarray[0] + ", stringarray[1 ]= "+ stringarray[1] +", stringarray[2]= "+ stringarray[2]);} catch (IOException e) {}return totalmemory/1024;} Private Long Getavailmemory () {memoryinfo memoryinfo = new Memoryinfo (); Mactivitymanager.getmemoryinfo (memoryinfo); Return MEMORYINFO.AVAILMEM/(1024 * 1024);}}

Main.xml as follows:

<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" > < TextView android:id= "@+id/totaltextview" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:text=" system Memory: "android:textsize=" 25SP "android:textcolor=" #1cf109 "/> <te Xtview android:id= "@+id/totalmemorytextview" android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" android:layout_torightof= "@id/totaltextview" android:textsize= "25SP" Android:textcol Or= "#1cf109"/> <textview android:id= "@+id/Availtextview "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layou t_below= "@id/totaltextview" android:text= "Available Memory:" android:textsize= "25SP" android:textcolor= "#5c0169" /> <textview android:id= "@+id/availmemorytextview" android:layout_width= "Wrap_content" Andro id:layout_height= "Wrap_content" android:layout_below= "@id/totaltextview" android:layout_torightof= "@id/avail TextView "android:textsize=" 25sp "android:textcolor=" #5c0169 "/> <button android:id=" @+id/cl Eanbutton "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_bel ow= "@id/availmemorytextview" android:textsize= "25sp" android:text= "clean Memory"/> <scrollview Android Oid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_below= "@id/cleanbutton" &        Gt <textview           Android:id= "@+id/cleaninfotextview" android:layout_width= "Fill_parent" Android:layout_hei ght= "Wrap_content"/> </ScrollView></RelativeLayout>


PS: Better way see Android Cleanup device Memory Detailed complete example (i)

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.