当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to retrieve available system ram and battery level

问题描述:

Im currently working on an app that I really need help with and I've come to the stage where I need a bit of help. Ok so I am trying to get the amount of free system memory/Ram and the battery level of the device and I want to display them in my app activity.

here is what I have so far.

MemoryInfo mi = new MemoryInfo();

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

activityManager.getMemoryInfo(mi);

long availableMegs = mi.availMem / 1048576L;

网友答案:

Register for this action in your activity Intent.ACTION_BATTERY_CHANGED

And write below code to get battery level from the broadcast receiver.

BroadcastReceiver mReceiver = new BroadcastReceiver() 
{
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        } 
    }
};

level value gives the battery level of your device.

分享给朋友:
您可能感兴趣的文章:
随机阅读: