Android清理裝置記憶體詳細完整樣本

來源:互聯網
上載者:User

MainActivity如下:

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描述: * 清理手機記憶體 *  * 參考資料: * 1 http://blog.30c.org/1816.html * 2 http://www.cnblogs.com/helloandroid/archive/2011/10/14/2212334.html *   Thank you very much *  * 注意許可權: *  *  */public class MainActivity extends Activity {private TextView mTotalMemoryTextView;private TextView mAvailMemoryTextView;private Button mCleanButton;private TextView mCleanInfoTextView;private ActivityManager mActivityManager;private StringBuffer mCleanInfoStringBuffer;private long availMemory;private long totalMemory;private List 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 serviceInfos = mActivityManager.getRunningServices(100);if (mRunningAppProcessInfoList != null) {for (int i = 0; i < mRunningAppProcessInfoList.size(); ++i) {runningAppProcessInfo= mRunningAppProcessInfoList.get(i);// 一般數值大於RunningAppProcessInfo.IMPORTANCE_SERVICE// 的進程即為長時間未使用進程或者空進程// 一般數值大於RunningAppProcessInfo.IMPORTANCE_VISIBLE// 的進程都是非可見進程,即在後台運行if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {String[] pkgList = runningAppProcessInfo.pkgList;for (int j = 0; j < pkgList.length; ++j) {mActivityManager.killBackgroundProcesses(pkgList[j]);mCleanInfoStringBuffer.append(pkgList[j] + " is killed...\n");mCleanInfoTextView.setText(mCleanInfoStringBuffer.toString());}}}}                //再次獲得剩餘記憶體以計算清理值mCleanInfoStringBuffer.append("共清理:"+(getAvailMemory() - availMemory) + "MB");mCleanInfoTextView.setText(mCleanInfoStringBuffer.toString());mAvailMemoryTextView.setText(getAvailMemory() + "MB");}});}private long getTotalMemory() {//系統的記憶體資訊檔String filePath = "/proc/meminfo";String lineString;String[] stringArray;long totalMemory = 0;try {FileReader fileReader = new FileReader(filePath);BufferedReader bufferedReader = new BufferedReader(fileReader,1024 * 8);//讀取meminfo第一行,擷取系統總記憶體大小lineString = bufferedReader.readLine();//按照空格拆分stringArray = lineString.split("\\s+");//獲得系統總記憶體,單位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);}}

            

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.