亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android獲取手機電池電量用法實例

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

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

原理概述:

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

當(dāng)需要在程序中獲取電池電量的信息時,需要為應(yīng)用程序注冊BroadcastReceiver組件,當(dāng)特定的Action事件發(fā)生時,系統(tǒng)將會發(fā)出相應(yīng)的廣播,應(yīng)用程序就可以通過BroadcastReceiver來接受廣播,并進(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);//注冊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+"%。");
    }
  }
}

運行結(jié)果:

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

相關(guān)文章

  • Android開源庫自定義相機模塊

    Android開源庫自定義相機模塊

    這篇文章主要為大家詳細(xì)介紹了Android開源庫自定義相機模塊,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android開發(fā)flow常見API的使用示例詳解

    Android開發(fā)flow常見API的使用示例詳解

    這篇文章主要為大家介紹了Android開發(fā)flow常見API的使用示例詳解,希望能夠幫助大家更好的掌握flow使用,熟練的應(yīng)用于各種場景,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android仿新版微信浮窗效果

    Android仿新版微信浮窗效果

    在新版微信中,可以把瀏覽的文章縮小為浮窗.點擊浮窗繼續(xù)閱讀.這篇文章主要介紹了Android仿新版微信浮窗效果,需要的朋友可以參考下
    2018-06-06
  • Android高手進(jìn)階教程(二十六)之---Android超仿Path菜單的功能實現(xiàn)!

    Android高手進(jìn)階教程(二十六)之---Android超仿Path菜單的功能實現(xiàn)!

    本篇文章主要主要介紹了Android超仿Path菜單的功能實現(xiàn),現(xiàn)在分享給大家,也給大家做個參考。感興趣的可以了解一下。
    2016-11-11
  • Android實現(xiàn)歷史搜索記錄

    Android實現(xiàn)歷史搜索記錄

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)歷史搜索記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android Studio安裝配置方法圖文詳細(xì)教程

    Android Studio安裝配置方法圖文詳細(xì)教程

    這篇文章主要為大家介紹了Android Studio下載和配置圖文詳細(xì)教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 詳解Android 硬布局item的高級寫法

    詳解Android 硬布局item的高級寫法

    這篇文章主要介紹了詳解Android 硬布局item的高級寫法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • android仿微信聊天界面 語音錄制功能

    android仿微信聊天界面 語音錄制功能

    這篇文章主要為大家詳細(xì)介紹了Android基于百度語音的語音交互功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android編程檢測手機錄音權(quán)限是否打開的方法

    Android編程檢測手機錄音權(quán)限是否打開的方法

    這篇文章主要介紹了Android編程檢測手機錄音權(quán)限是否打開的方法,涉及Android針對音頻操作的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2017-11-11
  • Android最簡單的狀態(tài)切換布局實現(xiàn)教程

    Android最簡單的狀態(tài)切換布局實現(xiàn)教程

    這篇文章主要給大家介紹了關(guān)于Android中最簡單的狀態(tài)切換布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10

最新評論