Android内存使用情况的应用实例
程序员文章站
2022-05-20 22:43:59
android内存使用情况的应用实例
实现效果图:
创建项目
android清单文件
android内存使用情况的应用实例
实现效果图:
创建项目
android清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.memorydemo" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="19" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.itheima28.memorydemo.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
布局文件
<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" tools:context="com.itheima28.memorydemo.mainactivity$placeholderfragment" > <textview android:id="@+id/tv_memory_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true"/> </relativelayout>
查询内存的代码
package com.itheima28.memorydemo; import java.io.file; import android.os.bundle; import android.os.environment; import android.os.statfs; import android.support.v7.app.actionbaractivity; import android.text.format.formatter; import android.widget.textview; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview tvmemoryinfo = (textview) findviewbyid(r.id.tv_memory_info); //获得sd卡的内存状态 file sdcardfiledir = environment.getexternalstoragedirectory(); string sdcardmemory = getmemoryinfo(sdcardfiledir); //获得手机内部存储控件的状态 file datafiledir = environment.getdatadirectory(); string datamemory = getmemoryinfo(datafiledir); tvmemoryinfo.settext("sd卡: " + sdcardmemory + "\n手机内部: " + datamemory); } /** * 根据路径获取内存状态 * @param path * @return */ @suppresswarnings("deprecation") private string getmemoryinfo(file path) { //获得一个磁盘状态对象 statfs stat = new statfs(path.getpath()); //获得一个扇区的大小 long blocksize = stat.getblocksize(); //获得扇区的总数 long totalblocks = stat.getblockcount(); //获得可用的扇区数量 long availableblocks = stat.getavailableblocks(); //总空间 string totalmemory = formatter.formatfilesize(this, totalblocks * blocksize); //可用空间 string availablememory = formatter.formatfilesize(this, availableblocks * blocksize); return "总空间:" + totalmemory + "\n可用空间:" + availablememory; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: DOS之del命令