日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

04_查看Android内存使用情况

發(fā)布時間:2024/9/27 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04_查看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>

  • 查詢內(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);

    ???

    ?????? //獲得手機內(nèi)部存儲控件的狀態(tài)

    ?????? File dataFileDir = Environment.getDataDirectory();

    ?????? String dataMemory = getMemoryInfo(dataFileDir);

    ?

    ?????? tvMemoryInfo.setText("SD: " + sdcardMemory + "\n手機內(nèi)部: " + dataMemory);

    ??? }

    ???

    ??? /**

    ??? ?* 根據(jù)路徑獲取內(nèi)存狀態(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;

    ??? }

    }

    ?

    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的04_查看Android内存使用情况的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。