Android內存使用情況的應用實例
更新時間:2017年04月14日 11:52:14 投稿:lqh
這篇文章主要介紹了Android內存使用情況的應用實例的相關資料,需要的朋友可以參考下
Android內存使用情況的應用實例
實現(xiàn)效果圖:
創(chuà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>
查詢內存的代碼
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卡的內存狀態(tài) File sdcardFileDir = Environment.getExternalStorageDirectory(); String sdcardMemory = getMemoryInfo(sdcardFileDir); //獲得手機內部存儲控件的狀態(tài) File dataFileDir = Environment.getDataDirectory(); String dataMemory = getMemoryInfo(dataFileDir); tvMemoryInfo.setText("SD卡: " + sdcardMemory + "\n手機內部: " + dataMemory); } /** * 根據(jù)路徑獲取內存狀態(tài) * @param path * @return */ @SuppressWarnings("deprecation") private String getMemoryInfo(File path) { //獲得一個磁盤狀態(tài)對象 StatFs stat = new StatFs(path.getPath()); //獲得一個扇區(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; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Android ViewPager導航小圓點實現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細介紹了Android ViewPager導航小圓點實現(xiàn)無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android ShimmerLayout實現(xiàn)微光效果解析
這篇文章主要為大家詳細介紹了Android ShimmerLayout實現(xiàn)微光效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03android設備不識別awk命令 缺少busybox怎么辦
這篇文章主要為大家詳細介紹了android設備不識別awk命令,缺少busybox的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04