Android清理設備內存具體完整演示樣例(二)

版權聲明: https://blog.csdn.net/lfdfhl/article/details/27672913

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* * 注意權限:* <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />* */
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<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(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);}}

main.xml例如以下:

<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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin" ><TextViewandroid:id="@+id/totalTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="系統內存:"android:textSize="25sp"android:textColor="#1cf109" /><TextViewandroid:id="@+id/totalMemoryTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/totalTextView"android:textSize="25sp"android:textColor="#1cf109" /><TextViewandroid:id="@+id/availTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/totalTextView"android:text="可用內存:"android:textSize="25sp"android:textColor="#5c0169" /><TextViewandroid:id="@+id/availMemoryTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/totalTextView"android:layout_toRightOf="@id/availTextView"android:textSize="25sp"android:textColor="#5c0169" /><Buttonandroid:id="@+id/cleanButton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/availMemoryTextView"android:textSize="25sp"android:text="清理內存" /><ScrollViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/cleanButton" ><TextViewandroid:id="@+id/cleanInfoTextView"android:layout_width="fill_parent"android:layout_height="wrap_content" /></ScrollView></RelativeLayout>


PS:更好的方式請參見Android清理設備內存具體完整演示樣例(一)

轉載于:https://www.cnblogs.com/mqxnongmin/p/10745174.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394155.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394155.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394155.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java圖片合成視頻_使用JAVACV把圖片合成視頻

使用JAVACV1.2把圖片合成視頻&#xff0c;直接上代碼。自己mark一下&#xff0c;也希望能夠幫助更多的人。package test;import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;import java.io.File;import org.bytedeco.javacpp.avcodec;import org.bytedeco.java…

NPOI導出Excel

首先在官網去下載NPOI&#xff0c;把dll引用到項目中&#xff0c;然后獲取列表調用下面的方法就可以導出 后臺代碼&#xff1a; /// <summary> /// NPOI導出Excel /// </summary> /// <param name"dt"></param> /// <param name"fil…

leetcode1028. 從先序遍歷還原二叉樹(dfs/棧)

我們從二叉樹的根節點 root 開始進行深度優先搜索。 在遍歷中的每個節點處&#xff0c;我們輸出 D 條短劃線&#xff08;其中 D 是該節點的深度&#xff09;&#xff0c;然后輸出該節點的值。&#xff08;如果節點的深度為 D&#xff0c;則其直接子節點的深度為 D 1。根節點的…

react jest測試_如何使用Jest和react-testing-library測試Socket.io-client應用程序

react jest測試by Justice Mba由Mba法官 如何使用Jest和react-testing-library測試Socket.io-client應用程序 (How to test a Socket.io-client app using Jest and the react-testing-library) Testing the quality of real-time Socket.io-client integration seems to have…

統計學會用到python嗎_統計學學的統計軟件深嗎(例如Python)普通一本統計學大一不知道該干什么?...

統計學的話&#xff0c;不考慮把基礎課和專業課好好學一學嘛&#xff5e; 大一的話數分高代幾何已經占了很長時間啦&#xff0c;多刷刷題&#xff0c;把績點和排名搞得高一點是重中之重嘛&#xff5e;再說學習語言的事兒&#xff5e; 要說日常使用&#xff0c;那還是更推薦pyth…

枚舉轉中文,通過反射方法與描述的方式獲取

示例&#xff1a; 有人為了顯示中文&#xff0c;這樣定義枚舉嗎&#xff1f; publicenum TimeOfDay { 上午, 下午, 晚上 }; 這樣定義&#xff0c;很別扭&#xff0c;特別是在使用的時候&#xff0c; 比如&#xff0c;this.Time TimeOfDay.上午; 而…

Java語言最新實用案例教程_Java 語言實用案例教程

基本信息書名:Java 語言實用案例教程出版價格&#xff1a;48元作者:常玉慧, 王秀梅出版社&#xff1a;科學出版社出版日期&#xff1a;2016-10-1ISBN&#xff1a;9787030497383字數&#xff1a;387000頁碼&#xff1a;235版次&#xff1a;版裝幀&#xff1a;平裝開本&#xff1…

(轉)Java隨機數

1 隨機數的三種產生方式 本章先講解Java隨機數的幾種產生方式&#xff0c;然后通過示例對其進行演示。 廣義上講&#xff0c;Java中的隨機數的有三種產生方式&#xff1a; (01). 通過System.currentTimeMillis()來獲取一個當前時間毫秒數的long型數字。(02). 通過Math.random()…

leetcode105. 從前序與中序遍歷序列構造二叉樹(遞歸)

根據一棵樹的前序遍歷與中序遍歷構造二叉樹。注意: 你可以假設樹中沒有重復的元素。例如&#xff0c;給出前序遍歷 preorder [3,9,20,15,7] 中序遍歷 inorder [9,3,15,20,7] 返回如下的二叉樹&#xff1a;3/ \9 20/ \15 7代碼 /*** Definition for a binary tree node.*…

途虎養車三個創始人_3個來自非常規創始人的獲獎技術和產品見解

途虎養車三個創始人by Henry通過亨利 3個來自非常規創始人的獲獎技術和產品見解 (3 Winning Technology & Product Insights from WeChat’s unconventional founder) Intro: The writer is a current PMLinkedIn. Formerly he worked as a growth engineer Facebook. he …

Powershell-創建Module

1.找到默認module路徑&#xff0c;ISE啟動時自動加載默認領下的Module代碼。 $env:PSModulePath 2.在其中一個默認路徑下創建個文件夾&#xff0c;在文件夾下創建一個.psm1后綴文件&#xff0c;注意文件夾名字與文件名一樣。 3.在.psm1文件中寫入函數代碼。 4.重啟ISE自動加載m…

android是java_為什么大家都用JAVA寫android程序

您好&#xff0c;1、原始類型&#xff1a;v void 只能用于返回值類型Z booleanB byteS shortC charI intJ long(64位)F floatD double(64位)對象類型&#xff1a;Lpackage/name/ObjectName相當于java中的package.name.ObjectName解釋如下&#xff1a;L&#xff1a;表示這是一個…

preserve log什么意思_一些有意思的JavaScript代碼片段

Javascript是一門很靈活的語言&#xff0c;我們可以使用它動態地實現各種各樣的功能。但是動態帶來便利的同時&#xff0c;也存在一些令人費解的行為&#xff0c;稍不注意就會進入誤區一個接著一個的坑。雖然我使用JavaScript的時間還不算長&#xff0c;也是遇到了一些有意思的…

快速排序——Java

快排的思想想必大家都懂&#xff0c;前后兩個指針&#xff0c;向中間靠攏。我這個partition函數能保證所有相同的數都被比較一次&#xff0c;靠攏在一起。 代碼&#xff1a; public class Main { public static int[] partition1(int[] arr, int begin, int end, int pivotVal…

預處理器sass_Sass — Web的預處理器裝飾

預處理器sass美學的重要性&#xff0c;其影響以及實現這一目標的工具。 (Importance of aesthetics, its impact, and tool to achieve it.) I remember as a child, every time I’d walk up to a bakery, I’d choose the pastries with the most beautiful toppings. Only a…

leetcode971. 翻轉二叉樹以匹配先序遍歷(dfs)

給定一個有 N 個節點的二叉樹&#xff0c;每個節點都有一個不同于其他節點且處于 {1, …, N} 中的值。 通過交換節點的左子節點和右子節點&#xff0c;可以翻轉該二叉樹中的節點。 考慮從根節點開始的先序遍歷報告的 N 值序列。將這一 N 值序列稱為樹的行程。 &#xff08;回…

【BZOJ3932】[CQOI2015]任務查詢系統 主席樹

【BZOJ3932】[CQOI2015]任務查詢系統 Description 最近實驗室正在為其管理的超級計算機編制一套任務管理系統&#xff0c;而你被安排完成其中的查詢部分。超級計算機中的任務用三元組(Si,Ei,Pi)描述&#xff0c;(Si,Ei,Pi)表示任務從第Si秒開始&#xff0c;在第Ei秒后結束&…

沖刺第一天

任務板 未開始 進行中已完成 劉曉杰&#xff1a;找回密碼界面 頁面風格優化 劉曉杰&#xff1a;滑動歡迎界面/加載界面 預計時間&#xff1a;5.5h 馮晨&#xff1a;找回密碼功能 發布動態界面 馮晨&#xff…

杭電1003 java_杭電ACM1003題怎么理解?

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓http://acm.hdu.edu.cn/showproblem.php?pid1003Max SumTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 99208 Accepted Submission(s): 22835Problem DescriptionGiven…

ubtunu打開firefox_如何在Firefox(在Lubuntu中)中打開“apt”鏈接?

問題描述Ask Ubuntu上的許多答案都直接指向在Ubuntu軟件中心中在Xubuntu中打開的this之類的鏈接。在Lubuntu中&#xff0c;我收到此錯誤消息&#xff1a;在Firefox-Preferences中&#xff0c;應用程序看不到類似于apt的東西來關聯程序等。在Chromium或Opera中打開相同的鏈接&am…