Android device memory and SD card Operation Tool Class

Source: Internet
Author: User

Package Cc.c;import Java.io.file;import Java.util.list;import android.os.statfs;import java.io.FileReader;import Java.io.ioexception;import Java.io.bufferedreader;import Android.os.environment;import Android.content.Context; Import Android.app.activitymanager;import Android.app.activitymanager.memoryinfo;import Android.app.activitymanager.runningappprocessinfo;public class Storageutil {private static final int ERROR = -1;public static int save_dir = 1;//Determine if SD card is installed public static Boolean issdcardexist () {return Android.os.Environment.getExternalStorageState (). Equals (Android.os.Environment.MEDIA_MOUNTED);} Memory Cleanup//Note permissions://<uses-permission android:name= "Android.permission.KILL_BACKGROUND_PROCESSES"/>public static void Cleanmemory (Context context) {SYSTEM.OUT.PRINTLN ("---> Pre-Clean memory available:" +getavailmemory (context) + "MB"); Activitymanager activitymanager= (Activitymanager) Context.getsystemservice (Context.activity_service); Runningappprocessinfo Runningappprocessinfo=null; List<runningappprocessInfo> runningappprocessinfolist= activitymanager.getrunningappprocesses ();//list< Activitymanager.runningserviceinfo> Serviceinfos = mactivitymanager.getrunningservices, if ( Runningappprocessinfolist! = null) {for (int i = 0; i < runningappprocessinfolist.size (); ++i) {runningappprocessinfo= Runningappprocessinfolist.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) {System.out.println ("===> is cleaning up:" +pkglist[j]); Activitymanager.killbackgroundprocesses (Pkglist[j]);}}} SYSTEM.OUT.PRINTLN ("---> Free memory after clean up:" +getavailmemory (context) + "MB");} Gets the total memory size public static 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 (); catch (IOException e) {}return totalmemory/1024;} Get available memory size public static long getavailmemory (context context) {Activitymanager activitymanager= (Activitymanager) Context.getsystemservice (Context.activity_service); Memoryinfo memoryinfo = new Memoryinfo () Activitymanager.getmemoryinfo (memoryinfo); return memoryinfo.availmem/(1024 * 1024);} SD Card remaining space public static long Getavailableexternalmemorysize () {if (Issdcardexist ()) {File Path = Environment.getexternalstoragedirectory (); StatFs stat = new StatFs (Path.getpath ()); Long blockSize = Stat.getblocksize (); Long availableblocks = Stat.getavailableblocks (); return availableblocks * blockSize;} else {return ERROR;}} SD Card Total Space public staticLong Gettotalexternalmemorysize () {if (Issdcardexist ()) {File path = Environment.getexternalstoragedirectory (); StatFs stat = new StatFs (Path.getpath ()); Long blockSize = Stat.getblocksize (); Long totalblocks = Stat.getblockcount (); return totalblocks * blockSize;} else {return ERROR;}} Determine the total size of the EXTERNAL_SD folder under the SD card public static long Gettotalexternal_sdmemorysize () {if (Issdcardexist ()) {File Path = Environment.getexternalstoragedirectory (); File EXTERNALSD = new file (Path.getpath () + "/EXTERNAL_SD"); if (externalsd.exists () && externalsd.isdirectory () {StatFs stat = new StatFs (Path.getpath () + "/EXTERNAL_SD"); Long blockSize = Stat.getblocksize (); Long totalblocks = Stat. Getblockcount (); if (Gettotalexternalmemorysize ()! = -1&& Gettotalexternalmemorysize ()! = totalBlocks* BlockSize) {return totalblocks * blockSize;} else {return ERROR;}} else {return ERROR;}} else {return ERROR;}} Determine the available size of the EXTERNAL_SD folder under the SD card public static long Getavailableexternal_sdmemorysize () {if (Issdcardexist ()) {File path = Environment.getexternalstoragedirectory (); File EXTERNALSD = new file (Path.getpath () + "/EXTERNAL_SD"); if (externalsd.exists () && externalsd.isdirectory () {StatFs stat = new StatFs (Path.getpath () + "/EXTERNAL_SD"); Long blockSize = Stat.getblocksize (); Long availableblocks = s Tat.getavailableblocks (); if (Getavailableexternalmemorysize ()! = -1&& Getavailableexternalmemorysize ()! = availableblocks* blockSize) {return availableblocks * blockSize;} else {return ERROR;}} else {return ERROR;}} else {return ERROR;}}}

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.