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)文章
Kotlin使用flow實(shí)現(xiàn)倒計(jì)時(shí)功能(示例詳解)
這篇文章主要介紹了Kotlin使用flow實(shí)現(xiàn)倒計(jì)時(shí)功能,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02
Android Fragment 和 FragmentManager 的代碼分析
這篇文章主要介紹了Android Fragment 和 FragmentManager 的代碼分析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
Android利用ViewPager實(shí)現(xiàn)帶小圓球的圖片滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android利用ViewPager實(shí)現(xiàn)帶小圓球的圖片滑動(dòng),并且只有第一次安裝app時(shí)才出現(xiàn)歡迎界面具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android側(cè)滑導(dǎo)航欄的實(shí)例代碼
這篇文章主要介紹了Android側(cè)滑導(dǎo)航欄的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android之ImageSwitcher的實(shí)例詳解
這篇文章主要介紹了Android之ImageSwitcher的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家理解這個(gè)控件的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
RecyclerView實(shí)現(xiàn)拖拽排序效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)拖拽排序效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android編程設(shè)計(jì)模式之解釋器模式詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之解釋器模式,詳細(xì)分析了Android解釋器模式的概念、原理、使用場(chǎng)景、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-12-12
Android 異步任務(wù)和消息機(jī)制面試題分析
這篇文章主要為大家介紹了Android 異步任務(wù)和消息機(jī)制面試題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Android 網(wǎng)絡(luò)請(qǐng)求框架解析之okhttp與okio
HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個(gè)高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp如何用于安卓網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考下2021-10-10

