Android获取手机电池电量用法实例

本文实例讲述了Android获取手机电池电量用法。分享给大家供大家参考。具体如下:

原理概述:

手机电池电量的获取在应用程序的开发中也很常用,Android系统中手机电池电量发生变化的消息是通过Intent广播来实现的,常用的Intent的Action有  Intent.ACTION_BATTERY_CHANGED(电池电量发生改变时)、Intent.ACTION_BATTERY_LOW(电池电量达到下限时)、和Intent.ACTION_BATTERY_OKAY(电池电量从低恢复到高时)。

当需要在程序中获取电池电量的信息时,需要为应用程序注册BroadcastReceiver组件,当特定的Action事件发生时,系统将会发出相应的广播,应用程序就可以通过BroadcastReceiver来接受广播,并进行相应的处理。

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");//获得当前电量 int total=intent.getExtras().getInt("scale");//获得总电量 int percent=current*100/total; tv.setText("现在的电量是"+percent+"%。"); } } }

运行结果:

希望本文所述对大家的Android程序设计有所帮助。

时间: 2024-07-30 04:38:08

Android获取手机电池电量用法实例的相关文章

Android获取手机电池电量用法实例_Android

本文实例讲述了Android获取手机电池电量用法.分享给大家供大家参考.具体如下: 原理概述: 手机电池电量的获取在应用程序的开发中也很常用,Android系统中手机电池电量发生变化的消息是通过Intent广播来实现的,常用的Intent的Action有  Intent.ACTION_BATTERY_CHANGED(电池电量发生改变时).Intent.ACTION_BATTERY_LOW(电池电量达到下限时).和Intent.ACTION_BATTERY_OKAY(电池电量从低恢复到高时). 当

Android开发之浏览器用法实例详解(调用uc,opera,qq浏览器访问网页)_Android

本文实例讲述了Android开发之浏览器用法.分享给大家供大家参考,具体如下: 一.启动android默认浏览器 Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse("http://www.jb51.net"); intent.setData(content_url); startActivity(inten

Android开发之TabActivity用法实例详解_Android

本文实例讲述了Android开发之TabActivity用法.分享给大家供大家参考,具体如下: 一.简介 TabActivity继承自Activity,目的是让同一界面容纳更多的内容.TabActivity实现标签页的功能,通过导航栏对各个页面进行管理. 二.XML布局文件 注意: 1.TabActivity的布局文件要求以TabHost作为XML布局文件的根. 2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>. 3.<TabWidg

Android开发之BroadcastReceiver用法实例分析_Android

本文实例讲述了Android开发中BroadcastReceiver用法.分享给大家供大家参考.具体分析如下: 在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制. Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的.我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息

Android开发之BroadcastReceiver用法实例分析

  本文实例讲述了Android开发中BroadcastReceiver用法.分享给大家供大家参考.具体分析如下: 在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制. Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的.我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递

Android开发之Location用法实例分析_Android

本文实例讲述了Android开发中Location用法.分享给大家供大家参考,具体如下: Location 在Android 开发中还是经常用到的,如通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发)等.而在Android 中通过LocationManager来获取Location .通常获取Location 有GPS 获取,WIFI 获取. 这边介绍一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度. 第一步:创

Android开发之ViewSwitcher用法实例_Android

本文实例讲述了Android开发之ViewSwitcher用法.分享给大家供大家参考,具体如下: android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View. ViewSwitcher的addView函数的代码如下: /** * {@inheritDoc} * * @throws IllegalStateException if this switcher already contains two childre

Android内容提供者ContentProvider用法实例分析_Android

本文实例讲述了Android内容提供者ContentProvider用法.分享给大家供大家参考,具体如下: PersonContentProvider内容提供者类 package com.ljq.db; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher;

Android开发中PopupWindow用法实例分析_Android

本文实例分析了Android开发中PopupWindow用法.分享给大家供大家参考,具体如下: private TextView tv_appmanager_title; private ListView lv_app_manager; private LinearLayout ll_appmanager_loading; private AppManagerProvider provider; private List<AppManagerInfo> infos ; private AppM