获取手机电池百分比和电池容量方法

在智能手机的开发过程中,经常需要获取手机的电池信息。其实获取的方法很多,下面介绍下方法。


一:首先介绍获取电池容量。例如java反射方式获取。代码如下

package com.example.jamesfan.getbatterycapacity;

import android.content.Context;


public class BatteryInfo {

    public String getCapaCity(Context context) {
        Object mPowerProfile;
        double mBatteryCapacity = 0;
        String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

        try {
            mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class)
                    .newInstance(context);


            mBatteryCapacity = (double) Class.forName(POWER_PROFILE_CLASS)
                    .getMethod("getBatteryCapacity")
                    .invoke(mPowerProfile);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return String.valueOf(mBatteryCapacity + " mAh");
    }

如果是做项目,可以在如下默认文件中修改。一般第三方软件都是通过读取这里获取电池容量的。

文件路径:frameworks\base\core\res\res\xml\power_profile.xml

默认情况下是 1000 mAh,一般手机厂商会进行修改,便于第三方应用读取

  <!-- This is the battery capacity in mAh (measured at nominal voltage) -->
  <item name="battery.capacity">1000</item>


二:其次是通过广播方式接受电池信息。如下代码

package com.example.jamesfan.getbatterycapacity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.Toast;

public class BatteryReceiver extends BroadcastReceiver {

    int mCurrentLevel = 0;
    int m_total = 0;
    String m_strPercent;

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if(action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED))
        {
            Log.i("james-fan","get battery change broad");
        }
        // mCurrentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        //m_total = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        mCurrentLevel = intent.getExtras().getInt("level");// 获得当前电量
        m_total = intent.getExtras().getInt("scale");// 获得总电量
        int percent = mCurrentLevel * 100 / m_total;
        m_strPercent =percent+ "%";
    }


    public int getCurrentLevel()
    {
        return mCurrentLevel;
    }

    public int getTotal()
    {
        return m_total;
    }

    public String getBatteryPercent()
    {
        return m_strPercent;
    }

三:接下来是主函数,广播采用的是动态注册方式。也可以采用静态注册方法。

package com.example.jamesfan.getbatterycapacity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    BatteryReceiver m_receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        m_receiver = new BatteryReceiver();
        registerReceiver(m_receiver, intentFilter);

        Button showBatteryCapacity = (Button) findViewById(R.id.showBatteryCapacity);
        Button showBatteryInfo = (Button) findViewById(R.id.showBatteryInfo);
        //showBatteryCapacity.setOnClickListener(this);

        showBatteryCapacity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BatteryInfo batteryInfo = new BatteryInfo();
                String strBatteryInfo = batteryInfo.getCapaCity(MainActivity.this);
                Toast.makeText(MainActivity.this, strBatteryInfo, Toast.LENGTH_SHORT).show();
            }
        });


        showBatteryInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str;
                str = "CurrentLevel =" + m_receiver.getCurrentLevel()
                        + " Total=" + m_receiver.getTotal()
                        + " percent=" + m_receiver.getBatteryPercent();

                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(m_receiver);
    }
}



猜你喜欢

转载自blog.csdn.net/fan380485838/article/details/80804736
今日推荐