MVP模式-设计问题(内存监控应用)

时间:2019-03-19 08:00:05

标签: android memory android-mvp

我正在构建一个小型应用程序,并希望监视使用的资源(定期(每60秒监视一次)该应用程序的内存使用并将其存储在SQLite数据库中作为历史记录报告)。
然后我想在图表概览中显示存储的信息,该概览展示了在一个选定的时间范围内(过去一小时,过去24小时,过去7天,过去30天)的内存使用情况。
 以下代码计算内存使用量

public class ResourcesUsageRunnable implements Runnable {
    //var running to start run the thread or stop it by set its value to false
    private static volatile boolean running = true;

    private Context context;

    public ResourcesUsageRunnable(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        while (running) {
            //calculate memory usage using Runtime instance
            final Runtime runtime = Runtime.getRuntime();
            final long usedMemInMB = (runtime.totalMemory() - runtime.freeMemory()) / 1048576L;
            final long maxHeapSizeInMB = runtime.maxMemory() / 1048576L;
            final long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;
            final long totalMemory=runtime.totalMemory()/ 1048576L;
            final long freeMemory=runtime.freeMemory()/ 1048576L;
            //create instance from MemoryMonitoring to set the memory usage value and store it in SQLite table
            MemoryMonitoring memoryMonitoring = new MemoryMonitoring(totalMemory, freeMemory, usedMemInMB, maxHeapSizeInMB, availHeapSizeInMB, Common.getTimeInSecond());


            //insert into SQLite MemoryMonitoring table
            new InsertAsyncTask(context).execute(memoryMonitoring);
            //Log.d("TTTTTTTTTTT",memoryMonitoring.getFreeMemory()+"");
            //sleep the thread for 60 second
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //start working
    public static void startThread() {
        running = true;
    }
    //stop working
    public static void stopThread() {
        running = false;
    }

}

我的问题是如何在MVP模式设计中实现计算内存,在图表中显示并保存到SQLite的过程,我对MVP还是陌生的

0 个答案:

没有答案