要获取手机的可用内存,首先要获取系统服务信息,

ActivityManagermyActivityManager =(ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);

然后获得MemoryInfo类型对象

ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

然后,使用getMemoryInfo(memoryInfo)方法获得系统可用内存,此方法将内存大小保存在memoryInfo对象上

myActivityManager.getMemoryInfo(memoryInfo) ;

然后,memoryInfo对象上的availmem值即为所求

long memSize = memoryInfo.availMem ;

字符类型转换 ,转换成MB格式。

String leftMemSize = Formatter.formatFileSize(getBaseContext(), memSize);


public staticStringformatFileSize(Contextcontext, long number)

Added in API level 3

Formats a content size to be in the form of bytes, kilobytes, megabytes, etc

Parameters
context Context to use to load the localized units
number size value to be formatted
Returns
  • formatted string with the number
第一个参数是上下文,第二个是需要转换格式的long类型的文件大小。最终返回类似 22KB、52Bytes,22MB的字符串。


java文件 MainActivity.java

package com.xujin.availablemem;import android.app.Activity;import android.app.ActivityManager;import android.os.Bundle;import android.text.format.Formatter;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private ActivityManager myActivityManager;private TextView leftMem;private Button refresh;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);leftMem = (TextView)findViewById(R.id.avaMem);refresh = (Button)findViewById(R.id.refresh);//获取系统服务信息myActivityManager =(ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);upDateMemInfo();refresh.setOnClickListener(new OnClickListener(){public void onClick(View source){upDateMemInfo();}});}//更新可用内存信息    public void upDateMemInfo(){        //获得MemoryInfo对象      ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();        //获得系统可用内存,保存在MemoryInfo对象上          myActivityManager.getMemoryInfo(memoryInfo) ;          long memSize = memoryInfo.availMem ;                    //字符类型转换         String leftMemSize = Formatter.formatFileSize(getBaseContext(), memSize);        leftMem.setText(leftMemSize);}}

xml文件,activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" ><LinearLayout     android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="当前系统可用内存为:" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"       android:layout_weight="1"        android:id="@+id/avaMem"/>    </LinearLayout>        <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="refresh"        android:id="@+id/refresh"/></LinearLayout>

最终结果:

Android开发学习笔记(十二) 获取系统可用内存_第1张图片


更多相关文章

  1. Android 系统菜单与自定义菜单
  2. android拍照造成内存泄露问题
  3. Android中图片占用内存的计算
  4. android性能之一:内存泄露、内存溢出的区别及原理分析
  5. 【源码分享下载】每日更新之Android项目源码---手机天气预报系统
  6. android 4.0.4系统下实现apk的静默安装和启动
  7. Android系统基础(01)简介
  8. Android 音频系统:从 AudioTrack 到 AudioFlinger(全)

随机推荐

  1. 男生鼻子怎么画?动漫人物鼻子的画法
  2. vue使用axios报404错误:Uncaught (in prom
  3. 我采访了一位 Pornhub 工程师,聊了这些纯
  4. 一个 Java 字符串到底有多少个字符?
  5. 别在 Java 代码里乱打日志了,这才是正确的
  6. 不想 CRUD 干到老,就来看看这篇 OOM 排查
  7. Django源代码-启动、接收、应用中间件链
  8. 适用于Windows和Mac的15种最佳免费代码编
  9. 无视社区担忧,JPMS(Jigsaw)将被提交公开预览
  10. java编程基础-Java多线程的调度