怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)查看內(nèi)存使用情況功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)建站長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為牙克石企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,牙克石網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
創(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;
}
}看完上述內(nèi)容,你們掌握怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)查看內(nèi)存使用情況功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站標(biāo)題:怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)查看內(nèi)存使用情況功能
當(dāng)前URL:http://chinadenli.net/article40/ipcpho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制開發(fā)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)