Android內(nèi)存使用情況的應(yīng)用實(shí)例
Android內(nèi)存使用情況的應(yīng)用實(shí)例
實(shí)現(xiàn)效果圖:
創(chuàng)建項(xiàng)目
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>
查詢內(nèi)存的代碼
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卡的內(nèi)存狀態(tài) File sdcardFileDir = Environment.getExternalStorageDirectory(); String sdcardMemory = getMemoryInfo(sdcardFileDir); //獲得手機(jī)內(nèi)部存儲(chǔ)控件的狀態(tài) File dataFileDir = Environment.getDataDirectory(); String dataMemory = getMemoryInfo(dataFileDir); tvMemoryInfo.setText("SD卡: " + sdcardMemory + "\n手機(jī)內(nèi)部: " + dataMemory); } /** * 根據(jù)路徑獲取內(nèi)存狀態(tài) * @param path * @return */ @SuppressWarnings("deprecation") private String getMemoryInfo(File path) { //獲得一個(gè)磁盤狀態(tài)對(duì)象 StatFs stat = new StatFs(path.getPath()); //獲得一個(gè)扇區(qū)的大小 long blockSize = stat.getBlockSize(); //獲得扇區(qū)的總數(shù) long totalBlocks = stat.getBlockCount(); //獲得可用的扇區(qū)數(shù)量 long availableBlocks = stat.getAvailableBlocks(); //總空間 String totalMemory = Formatter.formatFileSize(this, totalBlocks * blockSize); //可用空間 String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize); return "總空間:" + totalMemory + "\n可用空間:" + availableMemory; } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android基于IJKPlayer視頻播放器簡單封裝設(shè)計(jì)
這篇文章主要介紹了Android基于IJKPlayer視頻播放器簡單封裝設(shè)計(jì),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android ShimmerLayout實(shí)現(xiàn)微光效果解析
這篇文章主要為大家詳細(xì)介紹了Android ShimmerLayout實(shí)現(xiàn)微光效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01android設(shè)備不識(shí)別awk命令 缺少busybox怎么辦
這篇文章主要為大家詳細(xì)介紹了android設(shè)備不識(shí)別awk命令,缺少busybox的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04