android通过广播打印RAM信息

通过广播打印ram相关log

参数说明:

广播:com.android.settings.action.RAM_INFO

int型参数index:0 - 3h, 1 - 6h, 2 - 12h, 3 - 24h

代表过去时间app使用ram情况(平均/最大占用)

Index: frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java	(版本 10810)
+++ frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java	(版本 10827)
@@ -121,6 +121,7 @@
         

+        sBackgroundActionWhiteListArraySet.add("com.android.settings.action.RAM_INFO");
     }

 
Index: packages/apps/Settings/src/com/android/settings/applications/RamInfo.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/applications/RamInfo.java	(不存在的)
+++ packages/apps/Settings/src/com/android/settings/applications/RamInfo.java	(版本 10827)
@@ -0,0 +1,120 @@
+package com.android.settings.applications;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.text.format.Formatter;
+import android.text.format.Formatter.BytesResult;
+import android.util.Log;
+
+import com.android.settings.R;
+import com.android.settings.Utils;
+import com.android.settings.applications.ProcStatsData.MemInfo;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class RamInfo extends BroadcastReceiver {
+
+    private static String TAG ="RamInfo";
+
+    private PackageManager mPm;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.d(TAG, "onReceive:" + intent.getAction());
+
+        int index = intent.getIntExtra("index", 3); // 0 - 3h, 1 - 6h, 2 - 12h, 3 - 24h
+
+        mPm = context.getPackageManager();
+
+        ProcStatsData statsManager = new ProcStatsData(context, false);
+        statsManager.setDuration(ProcessStatsBase.sDurations[index]);
+
+        MemInfo memInfo = statsManager.getMemInfo();
+        double usedRam = memInfo.realUsedRam;
+        double totalRam = memInfo.realTotalRam;
+        double freeRam = memInfo.realFreeRam;
+        BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
+                Formatter.FLAG_SHORTER);
+        String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
+        String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
+        CharSequence memString;
+        CharSequence[] memStatesStr = context.getResources().getTextArray(R.array.ram_states);
+        int memState = statsManager.getMemState();
+        if (memState >= 0 && memState < memStatesStr.length - 1) {
+            memString = memStatesStr[memState];
+        } else {
+            memString = memStatesStr[memStatesStr.length - 1];
+        }
+        String durationString = context.getResources().getString(ProcessStatsBase.sDurationLabels[index]);
+        int numApps = statsManager.getEntries().size();
+
+        Log.d(TAG, "********** RAM INFO (" + durationString + ") **********");
+        // Average
+        Log.d(TAG, context.getResources().getString(R.string.memory_avg_use) + ": " + usedResult.value + usedResult.units);
+        // Performance
+        Log.d(TAG, context.getResources().getString(R.string.memory_performance) + ": " + memString);
+        // Total memory
+        Log.d(TAG, context.getResources().getString(R.string.total_memory) + ": " + totalString);
+        // Average used
+        Log.d(TAG, context.getResources().getString(R.string.average_used) + ": " + (Utils.formatPercentage((long) usedRam, (long) totalRam)));
+        // Free
+        Log.d(TAG, context.getResources().getString(R.string.free_memory) + ": " + freeString);
+        // Memory used by apps
+        Log.d(TAG, context.getResources().getString(R.string.memory_usage_apps)+ ": " + context.getResources().getQuantityString(
+                R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
+
+        boolean mShowMax = false;
+
+        List<ProcStatsPackageEntry> pkgEntries = statsManager.getEntries();
+
+        // Update everything and get the absolute maximum of memory usage for scaling.
+        for (int i = 0, N = pkgEntries.size(); i < N; i++) {
+            ProcStatsPackageEntry pkg = pkgEntries.get(i);
+            pkg.updateMetrics();
+        }
+
+        Collections.sort(pkgEntries, mShowMax ? sMaxPackageEntryCompare : sPackageEntryCompare);
+
+        for (int i = 0; i < pkgEntries.size(); i++) {
+            ProcStatsPackageEntry pkg = pkgEntries.get(i);
+            pkg.retrieveUiData(context, mPm);
+            boolean statsForeground = pkg.mRunWeight > pkg.mBgWeight;
+            double avgAmount = (statsForeground ? pkg.mRunWeight : pkg.mBgWeight) * memInfo.weightToRam;
+            double maxAmount = (statsForeground ? pkg.mMaxRunMem : pkg.mMaxBgMem) * memInfo.totalScale * 1024;
+            Log.d(TAG, pkg.mUiLabel + "(" + pkg.mPackage + ")" + 
+                    " {" + context.getResources().getString(R.string.memory_avg_use) + ":" + Formatter.formatShortFileSize(context, (long) avgAmount) + " " +
+                    context.getResources().getString(R.string.memory_max_use) + ":" + Formatter.formatShortFileSize(context, (long) maxAmount) + "}");
+        }
+        Log.d(TAG, "********** RAM INFO END **********");
+    }
+
+    final static Comparator<ProcStatsPackageEntry> sPackageEntryCompare
+            = new Comparator<ProcStatsPackageEntry>() {
+        @Override
+        public int compare(ProcStatsPackageEntry lhs, ProcStatsPackageEntry rhs) {
+            double rhsWeight = Math.max(rhs.mRunWeight, rhs.mBgWeight);
+            double lhsWeight = Math.max(lhs.mRunWeight, lhs.mBgWeight);
+            if (lhsWeight == rhsWeight) {
+                return 0;
+            }
+            return lhsWeight < rhsWeight ? 1 : -1;
+        }
+    };
+
+    final static Comparator<ProcStatsPackageEntry> sMaxPackageEntryCompare
+            = new Comparator<ProcStatsPackageEntry>() {
+        @Override
+        public int compare(ProcStatsPackageEntry lhs, ProcStatsPackageEntry rhs) {
+            double rhsMax = Math.max(rhs.mMaxBgMem, rhs.mMaxRunMem);
+            double lhsMax = Math.max(lhs.mMaxBgMem, lhs.mMaxRunMem);
+            if (lhsMax == rhsMax) {
+                return 0;
+            }
+            return lhsMax < rhsMax ? 1 : -1;
+        }
+    };
+}
\ No newline at end of file
Index: packages/apps/Settings/AndroidManifest.xml
===================================================================
--- packages/apps/Settings/AndroidManifest.xml	(版本 10810)
+++ packages/apps/Settings/AndroidManifest.xml	(版本 10827)
@@ -3603,5 +3603,11 @@
             </intent-filter>
         </receiver> 

+
+        <receiver android:name=".applications.RamInfo">
+            <intent-filter>    
+                <action android:name="com.android.settings.action.RAM_INFO" />
+            </intent-filter>
+        </receiver> 
     </application>
 </manifest>
相关推荐
weixin_4493108426 分钟前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
Zender Han41 分钟前
Flutter自定义矩形进度条实现详解
android·flutter·ios
白乐天_n3 小时前
adb:Android调试桥
android·adb
姑苏风7 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k10 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小1011 小时前
JavaWeb项目-----博客系统
android
风和先行11 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.12 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰13 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶13 小时前
Android——网络请求
android