Android如何得到电量、温度、电压

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/libeifs/article/details/6630281

开发环境

Window 7

Eclipse helios

MB525 defy  Android 2.1-update1


羽化博客的第三篇,由于这个星期兄弟过来玩,所以决定选个简单的程序来写博客,Android技术群里面也有人问过,网上资料也很多,原来写这个程序目的就是随便试试,这次一切从简吧~ ~好了 送上程序代码:

 

com.LB里面主程序Battery代码:

package com.LB;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

public class Battery extends Activity 
{
	private int BatteryN;		//目前电量
	private int BatteryV;		//电池电压
	private double BatteryT;		//电池温度
	private String BatteryStatus;	//电池状态
	private String BatteryTemp;		//电池使用情况

	public TextView TV;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 注册一个系统 BroadcastReceiver,作为访问电池计量之用这个不能直接在AndroidManifest.xml中注册
		registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		
		TV = (TextView)findViewById(R.id.TV);
		
	}
	
	/* 创建广播接收器 */
	private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() 
	{
		public void onReceive(Context context, Intent intent) 
		{
			String action = intent.getAction();
			/*
			 * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()
			 */
			if (Intent.ACTION_BATTERY_CHANGED.equals(action)) 
			{
				BatteryN = intent.getIntExtra("level", 0);	  //目前电量
				BatteryV = intent.getIntExtra("voltage", 0);  //电池电压
				BatteryT = intent.getIntExtra("temperature", 0);  //电池温度
				
				switch (intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)) 
				{
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    BatteryStatus = "充电状态";
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    BatteryStatus = "放电状态";
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    BatteryStatus = "未充电";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    BatteryStatus = "充满电";
                    break;
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    BatteryStatus = "未知道状态";
                    break;
				}
				
				switch (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)) 
				{
                case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                    BatteryTemp = "未知错误";
                    break;
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    BatteryTemp = "状态良好";
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    BatteryTemp = "电池没有电";
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    BatteryTemp = "电池电压过高";
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    BatteryTemp =  "电池过热";
                    break;
                }
		TV.setText("目前电量为" + BatteryN + "% --- " + BatteryStatus + "\n" + "电压为" + BatteryV + "mV --- " + BatteryTemp + "\n" + "温度为" + (BatteryT*0.1) + "℃");
			}
		}
	};
}


layout的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"
    >
<TextView
	android:id = "@+id/TV"
	android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>


好了,很简单的代码,至于为什么要传入数值怎么写大家可以看看帮助文档里面的BatteryManager  ---  The BatteryManager class contains strings and constants used for values in the ACTION_BATTERY_CHANGED Intent. 原来看mars的视频里面学到的最重要的不是如何开发Android而是学会使用帮助文档,也推荐大家看看这个学习视频,自我认为看视频比看书学得快-0-

 

下集预告

Android Unity3D游戏开发之躲避方块(自己瞎写的- -)

展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页