有没有什么方法能够快速,实时的获取android手机的电量值

有没有什么方法能够快速,实时的获取android手机的电量值,第1张

这个是获取android电量的DEMO:

package comandroidbatterywaster;

import androidappActivity;

import androidcontentBroadcastReceiver;

import androidcontentContext;

import androidcontentIntent;

import androidcontentIntentFilter;

import androidosBatteryManager;

import androidosBundle;

import androidosPowerManager;

import androidviewView;

import androidwidgetCheckBox;

import androidwidgetTextView;

import javatextDateFormat;

import javautilDate;

/

So you thought sync used up your battery life

/

public class BatteryWaster extends Activity {

TextView mLog;

DateFormat mDateFormat;

IntentFilter mFilter;

PowerManagerWakeLock mWakeLock;

SpinThread mThread;

@Override

public void onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState);

// Set the layout for this activity You can find it

// in res/layout/hello_activityxml

setContentView(Rlayoutmain);

findViewById(Ridcheckbox)setOnClickListener(mClickListener);

mLog = (TextView)findViewById(Ridlog);

mDateFormat = DateFormatgetInstance();

mFilter = new IntentFilter();

mFilteraddAction(IntentACTION_BATTERY_CHANGED);

mFilteraddAction(IntentACTION_BATTERY_LOW);

mFilteraddAction(IntentACTION_BATTERY_OKAY);

mFilteraddAction(IntentACTION_POWER_CONNECTED);

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);

mWakeLock = pmnewWakeLock(PowerManagerFULL_WAKE_LOCK, "BatteryWaster");

mWakeLocksetReferenceCounted(false);

}

@Override

public void onPause() {

stopRunning();

}

ViewOnClickListener mClickListener = new ViewOnClickListener() {

public void onClick(View v) {

CheckBox checkbox = (CheckBox)v;

if (checkboxisChecked()) {

startRunning();

} else {

stopRunning();

}

}

};

void startRunning() {

log("Start");

registerReceiver(mReceiver, mFilter);

mWakeLockacquire();

if (mThread == null) {

mThread = new SpinThread();

mThreadstart();

}

}

void stopRunning() {

log("Stop");

unregisterReceiver(mReceiver);

mWakeLockrelease();

if (mThread != null) {

mThreadquit();

mThread = null;

}

}

void log(String s) {

mLogsetText(mLoggetText() + "\n" + mDateFormatformat(new Date()) + ": " + s);

}

BroadcastReceiver mReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intentgetAction();

String title = action;

int index = titlelastIndexOf('');

if (index >= 0) {

title = titlesubstring(index + 1);

}

if (IntentACTION_BATTERY_CHANGEDequals(action)) {

int level = intentgetIntExtra(BatteryManagerEXTRA_LEVEL, -1);

int icon = intentgetIntExtra(BatteryManagerEXTRA_ICON_SMALL,-1);

log(title + ": level=" + level + "\n" + "icon:" + icon);

} else {

log(title);

}

}

};

class SpinThread extends Thread {

private boolean mStop;

public void quit() {

synchronized (this) {

mStop = true;

}

}

public void run() {

while (true) {

synchronized (this) {

if (mStop) {

>

BatteryManagerEXTRA_PRESENT :表示电池监听器,

if (!IntentACTION_BATTERY_CHANGEDequals(action))

return;

health=intentgetIntExtra(BatteryManagerEXTRA_HEALTH,BatteryManagerBATTERY_HEALTH_UNKNOWN);

batteryIconId=intentgetIntExtra(BatteryManagerEXTRA_ICON_SMALL,-1);

plugged=intentgetIntExtra(BatteryManagerEXTRA_PLUGGED, -1);

batteryPresent=intentgetBooleanExtra(BatteryManagerEXTRA_PRESENT, false);

status=intentgetIntExtra(BatteryManagerEXTRA_STATUS, BatteryManagerBATTERY_STATUS_UNKNOWN);

technology=intentgetStringExtra(BatteryManagerEXTRA_TECHNOLOGY);

temperature=intentgetIntExtra(BatteryManagerEXTRA_TEMPERATURE, -1);

voltage=intentgetIntExtra(BatteryManagerEXTRA_VOLTAGE, -1);

level = intentgetIntExtra(BatteryManagerEXTRA_LEVEL, 0);

scale = intentgetIntExtra(BatteryManagerEXTRA_SCALE, 100);

}

其返回值是boolean指。

可以通过intentgetBooleanExtra(BatteryManagerEXTRA_PRESENT,false);修改该值。是java的一种。

Battery Status API 向 windownavigator 扩展了一个 navigatorgetBattery 方法,其返回了一个battery promise, 完成后传递一个 BatteryManager 对象,并提供了一些新的可以 *** 作电池状态的事件。

我们可以借助这个web接口在适当情况下将其应用,例如:“在设备电量低的时候调整应用的资源使用状态,或者在电池用尽前保存应用中的修改以防数据丢失,提醒用户。”

通过navigatorgetBattery方法,我们可以实时监听放电状态和电池等级和剩余的事件(不论是否正在充电还是在使用电池)。

navigatorgetBattery 方法返回一个promise对象,该promise将提供一个BatteryManager接口,可以从Battery Status API 查询到相关信息。

如图所示,我们切换电源连接状态看效果:

BatteryManagercharging// 一个布尔值,说明当前电池是否正在充电。BatteryManagerchargingTime// 一个数字,代表距离充电完毕还需多少秒,如果为0则充电完毕。BatteryManagerdischargingTime

// 一个数字,代表距离电池耗电至空且挂起需要多少秒。BatteryManagerlevel

// 一个数字,代表电量的放大等级,这个值在 00 至 10 之间。

BatteryManageronchargingchange// chargingchange事件处理器;电池充电状态更新时被调用。BatteryManageronchargingtimechange// chargingtimechange事件处理器;电池充电时间更新时被调用。BatteryManagerondischargingtimechange// dischargingtimechange事件处理器;电池断开充电时间更新时被调用。BatteryManageronlevelchange// levelchange事件处理器;电池电量更新时被调用

DEMO:

{margin:0;padding:0;color:white;font-size:2vw;overflow:hidden;}wrap{position:relative;width:100vw;height:100vh;background:blue;}info{

 position:absolute;

 left:50%;

 top:50%;

 transform: translate(-50%,-50%);

 text-align:center;}info p{margin-top:2vh;}info p small{font-size:15vw;color:#f5f5f5;}

BODY<section class="wrap">

 <div class="info">

   <h1>当前电量<span id="BatterylevelText"></span></h1>

   <progress id="Batterylevel" value="0" max="100"></progress>

   <p>当前电源状态:<span id="Batterycharging">未知</span></p>

   <p>

     <small>chargingTime: <i id="Batterychargingtime"></i></small>

   </p>

   <p>

     <small>dischargingTime: <i id="Batterydischargingtime"></i></small>

   </p>

 </div></section>/BODY

navigatorgetBattery()then(function(battery) {

   documentquerySelector('#Batterylevel')value = batterylevel 100;

   documentquerySelector('#BatterylevelText')textContent = (batterylevel 100)  '%';

   documentquerySelector('#Batterycharging')textContent = batterycharging '电源已连接' : '电源已断开';

   documentquerySelector('#Batterychargingtime')textContent = batterychargingTime   " seconds"

   documentquerySelector('#Batterydischargingtime')textContent = batterydischargingTime   " seconds"

   // chargingchange事件处理器;电池充电状态更新时被调用。

   batteryaddEventListener('chargingchange', function() {

     documentquerySelector('#Batterycharging')textContent = batterycharging '电源已连接' : '电源已断开';

   });

   // levelchange事件处理器;电池电量更新时被调用。

   batteryaddEventListener('levelchange', function() {

     documentquerySelector('#Batterylevel')textContent = batterylevel 100;

     documentquerySelector('#BatterylevelText')textContent = (batterylevel 100)  '%';

   });

   // chargingtimechange事件处理器;电池充电时间更新时被调用。

   batteryaddEventListener('chargingtimechange', function() {

     documentquerySelector('#Batterychargingtime')textContent = batterychargingTime   " seconds"

   });

   // dischargingtimechange事件处理器;电池断开充电时间更新时被调用。

   batteryaddEventListener('dischargingtimechange', function() {

     documentquerySelector('#Batterydischargingtime')textContent = batterydischargingTime   " seconds"

   });

 });

测试后chargingTime与dischargingTime值是Infinity

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9301207.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存