最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取手機(jī)電池電量用法實(shí)例

 更新時(shí)間:2015年09月26日 16:04:35   作者:Ruthless  
這篇文章主要介紹了Android獲取手機(jī)電池電量用法,以完整實(shí)例形式較為詳細(xì)的分析了Android獲取手機(jī)電量的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android獲取手機(jī)電池電量用法。分享給大家供大家參考。具體如下:

原理概述:

手機(jī)電池電量的獲取在應(yīng)用程序的開(kāi)發(fā)中也很常用,Android系統(tǒng)中手機(jī)電池電量發(fā)生變化的消息是通過(guò)Intent廣播來(lái)實(shí)現(xiàn)的,常用的Intent的Action有  Intent.ACTION_BATTERY_CHANGED(電池電量發(fā)生改變時(shí))、Intent.ACTION_BATTERY_LOW(電池電量達(dá)到下限時(shí))、和Intent.ACTION_BATTERY_OKAY(電池電量從低恢復(fù)到高時(shí))。

當(dāng)需要在程序中獲取電池電量的信息時(shí),需要為應(yīng)用程序注冊(cè)BroadcastReceiver組件,當(dāng)特定的Action事件發(fā)生時(shí),系統(tǒng)將會(huì)發(fā)出相應(yīng)的廣播,應(yīng)用程序就可以通過(guò)BroadcastReceiver來(lái)接受廣播,并進(jìn)行相應(yīng)的處理。

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ToggleButton android:id="@+id/tb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:textOn="停止獲取電量信息"
    android:textOff="獲取電量信息" />
  <TextView android:id="@+id/tv" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

BatteryActivity類:

package com.ljq.activity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class BatteryActivity extends Activity {
  private ToggleButton tb=null;
  private TextView tv=null;
  private BatteryReceiver receiver=null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    receiver=new BatteryReceiver();
    tv=(TextView)findViewById(R.id.tv);
    tb=(ToggleButton)findViewById(R.id.tb);
    tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
      public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        //獲取電池電量
        if(isChecked){
          IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
          registerReceiver(receiver, filter);//注冊(cè)BroadcastReceiver
        }else {
          //停止獲取電池電量
          unregisterReceiver(receiver);
          tv.setText(null);
        }
      }
    });
  }
  private class BatteryReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
      int current=intent.getExtras().getInt("level");//獲得當(dāng)前電量
      int total=intent.getExtras().getInt("scale");//獲得總電量
      int percent=current*100/total;
      tv.setText("現(xiàn)在的電量是"+percent+"%。");
    }
  }
}

運(yùn)行結(jié)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

新津县| 华坪县| 永嘉县| 婺源县| 松原市| 涪陵区| 通道| 吉木萨尔县| 承德县| 商城县| 福海县| 商南县| 门源| 青海省| 达日县| 昌江| 嘉善县| 邯郸市| 石嘴山市| 奇台县| 清水河县| 永年县| 马鞍山市| 休宁县| 沾化县| 偏关县| 郎溪县| 墨竹工卡县| 柯坪县| 新平| 昌平区| 高碑店市| 定西市| 遵义县| 华池县| 桃江县| 福海县| 独山县| 察哈| 托克逊县| 谷城县|