is in Larger Memory Mode - Android Hardware

Android examples for Hardware:Memory

Description

is in Larger Memory Mode

Demo Code


//package com.java2s;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    static float memsize = -1.0f;

    public static boolean LargerMemoryMode(Context context) {
        if (false) {
            if (memsize == -1.0f) {
                memsize = getMemorySize(context);
                Log.d("memory", "size = " + memsize);
            }/*from  w w  w . j a  v  a  2 s . com*/
            return memsize >= 1.75f;
        }

        return true;
    }

    private static float getMemorySize(Context context) {
        if (Build.VERSION.SDK_INT >= 16) {
            ActivityManager actManager = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
            actManager.getMemoryInfo(memInfo);
            float totalMemory = (memInfo.totalMem / 1073741824.0f);
            return totalMemory;
        } else {
            return getTotalRAM();
        }
    }

    public static float getTotalRAM() {
        RandomAccessFile reader = null;
        String load = null;
        DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
        double totRam = 0;
        float lastValue = 0;
        try {
            reader = new RandomAccessFile("/proc/meminfo", "r");
            load = reader.readLine();

            // Get the Number value from the string
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(load);
            String value = "";
            while (m.find()) {
                value = m.group(1);
                // System.out.println("Ram : " + value);
            }
            reader.close();

            totRam = Double.parseDouble(value);
            // totRam = totRam / 1024;

            float mb = (float) (totRam / 1024.0f);
            float gb = (float) (totRam / 1048576.0f);
            float tb = (float) (totRam / 1073741824.0f);

            lastValue = gb;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Streams.close(reader);
        }

        return lastValue;
    }
}

Related Tutorials