android 静态注册获取电量,获取手机电池百分比和电池容量方法

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

一:首先介绍获取电池容量。例如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,一般手机厂商会进行修改,便于第三方应用读取

1000item>

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

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值