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;
@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<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+");
//获得系统总内存,单位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);
}
}
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" >
<TextView
android:id="@+id/totalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统内存:"
android:textSize="25sp"
android:textColor="#1cf109" />
<TextView
android:id="@+id/totalMemoryTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/totalTextView"
android:textSize="25sp"
android:textColor="#1cf109" />
<TextView
android: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" />
<TextView
android: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" />
<Button
android:id="@+id/cleanButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/availMemoryTextView"
android:textSize="25sp"
android:text="清理内存" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cleanButton" >
<TextView
android:id="@+id/cleanInfoTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
PS:更好的方式请參见Android清理设备内存具体完整演示样例(一)
Android清理设备内存具体完整演示样例(二)的更多相关文章
- Android清理设备内存具体完整演示样例(一)
MainActivity例如以下: package come.on; import android.app.Activity; import android.content.Context; impo ...
- FutureTask使用完整演示样例
MainActivity例如以下: package cc.cv; import java.util.concurrent.FutureTask; import android.os.Bundle; i ...
- Android利用Volley异步载入数据完整具体演示样例(二)
MainActivity例如以下: package cc.y; import android.app.Activity; import android.content.Context; import ...
- Android平台调用Web Service:演示样例
近期在学习Android,随着移动设备的流行,当软件走上商业化的道路,为了争夺市场,肯定须要支持Android的,所以開始接触了Android,只是仅仅了解皮毛就好,由于我们要做管理者嘛,懂点Andr ...
- 通过Canvas及File API缩放并上传图片完整演示样例
创建一个只管的用户界面,并同意你控制图片的大小.上传到server端的数据,并不须要处理enctype为 multi-part/form-data 的情况.只一个简单的POST表单处理程序就能够了. ...
- Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix
MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...
- Android之——流量管理程序演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...
- 构造Scala开发环境并创建ApiDemos演示样例项目
从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...
- 【Unity 3D 游戏开发】Unity3D 入门 - 工作区域介绍 与 入门演示样例
一. 工作区域具体解释 1. Scence视图 (场景设计面板) scence视图简单介绍 : 展示创建的游戏对象, 能够对全部的游戏对象进行 移动, 操作 和 放置; -- 演示样例 : 创建一个球 ...
随机推荐
- 常见HTTP状态码及URL编码表
常见HTTP状态码 1xx: 信息 (用于表示临时响应并需要请求者执行操作才能继续的状态代码) 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有 ...
- Linux 使用 ssh 命令远程连接另一台 Linux
用 Linux 系统的 ssh 命令远程连接另一台 Linux 机器的命令 #ssh 用户名@主机名(IP地址) 例: #ssh root@10.41.24.138 ...
- 微服务学习二:springboot与swagger2的集成
现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处 ...
- C#利用SerialPort控件进行串口编程小记
一.关于DataReceive事件. 主程序必须有 outserialPort.DataReceived +=new SerialDataReceivedEventHandler(outserialP ...
- forfiles
关键命令就这一条了:forfiles.exe /p "D:\Kugou" /m *.tar /d -14 /c "cmd /c del @path" 这条命令的 ...
- 菜鸟入门【ASP.NET Core】4:在CentOS上安装.NET Core运行时、部署到CentOS
下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows 第一步:Add the dotnet product feed( ...
- mysql 8小时超时设置
1.打开MySQL配置文件 2.添加 interactive_timeout=31536000wait_timeout=31536000 3.重新启动服务 打开MySQL命令行界面查看设置是否成功
- 【IDEA&&Eclipse】5、IntelliJ IDEA常见配置
[idea配置jdk] http://blog.csdn.net/tolcf/article/details/50803414 [idea intellij 如何配置tomcat]http://jin ...
- 通过swagger将API业务版本号与Gitlab代码版本号绑定
1.调用Gitlab API获取项目commit ID 2.编辑 Swagger2.java @Configuration @EnableSwagger2 @EnableWebMvc public c ...
- 解决vue-cli不能初始化webpack模板的问题(vue init卡住了,解决办法)
报这个错误 有人说是代理问题.我也不懂,但这个方法有用 1.去github上下载要初始化的模板 https://github.com/vuejs-templates/webpack 或者直接用git去 ...