Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android清理設備內存詳細完整示例

Android清理設備內存詳細完整示例

編輯:關於Android編程

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;
	@Override
	protected 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() {
			@Override
			public 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+");
			//獲得系統總內存,單位KB
			totalMemory = 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);
	}


}



    

    

    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved