首页 > 系统 > Android > 正文

Android内存使用情况的应用实例

2019-12-12 03:05:42
字体:
来源:转载
供稿:网友

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; }}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表