国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > 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; }}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 翁源县| 淮北市| 玉门市| 卓资县| 丹江口市| 广宗县| 邵阳市| 龙陵县| 临邑县| 石屏县| 玛纳斯县| 文昌市| 元阳县| 建瓯市| 亳州市| 兰西县| 镇远县| 乳山市| 西贡区| 通城县| 东丽区| 丹江口市| 金昌市| 乌拉特后旗| 蓝田县| 任丘市| 当涂县| 乌拉特后旗| 成都市| 麟游县| 潮州市| 黄梅县| 长葛市| 鲁山县| 获嘉县| 财经| 镇江市| 桂东县| 四川省| 沙坪坝区| 旬邑县|