一、在AndroidManifest.xml中使用电源管理权限
Android:name="android.permission.DEVICE_POWER"
/>
就会报错:Permission is only granted to system apps
原因如下:
此类权限仅授予系统级应用,可以修改下Link Error Checking项的安全级别;
In Eclipse: Window -> Preferences -> Android -> Lint Error Checking
在ID列表中,找到ID = ProtectedPermission,设置Serverity低于Error,比如Warning级别就好了。
二、android 电源状态获取
1、布局文件: activity_power.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/textStateId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电量状态:" />
二、AndroidMainfest.xml 添加了2个获取电量状态权限
package="com.example.powertest"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="10"
android:targetSdkVersion="10" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.powertest.Power"
android:label="@string/app_name" >
三、Power.java 获取电量信息
package com.example.powertest;
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.util.Log;
import android.widget.TextView;
public class Power extends Activity {
private String powerStr="";
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_power);
IntentFilter mIntentFilter = new IntentFilter();
//获取电量操作
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mIntentReceiver, mIntentFilter);
text = (TextView)findViewById(R.id.textStateId);
}
//接收获取电量的广播消息
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int type=0;
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
powerStr ="当前电量:" + intent.getIntExtra("level", 0)+"
";
//电池最大容量
powerStr += "最大电池电量" + intent.getIntExtra("scale", 0)+"
";
//电池伏数
powerStr += "电池伏数" + intent.getIntExtra("voltage", 0)+"
";
//电池温度
powerStr += "电池温度" + intent.getIntExtra("temperature", 0)+"
";
type = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
if(type==BatteryManager.BATTERY_STATUS_CHARGING)
{
powerStr += "电池状态:" + "充电状态"+"
";
}else if(type==BatteryManager.BATTERY_STATUS_DISCHARGING)
{
powerStr += "电池状态:" + "放电中"+"
";
}else if(type==BatteryManager.BATTERY_STATUS_NOT_CHARGING)
{
powerStr += "电池状态:" + "未充电"+"
";
}else if(type==BatteryManager.BATTERY_STATUS_FULL)
{
powerStr += "电池状态:" + "电池满"+"
";
}
type=intent.getIntExtra("plugged", 0);
Log.e("Battery", "" + intent.getIntExtra("plugged", 0));
if(BatteryManager.BATTERY_PLUGGED_AC==type)
{
powerStr += "充电类型:充电器" +"
";
}else{
powerStr += "充电类型:usb" +"
";
}
type=intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
if(type==BatteryManager.BATTERY_HEALTH_GOOD)
{
powerStr += "电池健康情况:良好" +"
";
}else if(type==BatteryManager.BATTERY_HEALTH_OVERHEAT)
{
powerStr += "电池健康情况:过热" +"
";
}else if(type==BatteryManager.BATTERY_HEALTH_DEAD)
{
powerStr += "电池健康情况:没电" +"
";
}else if(type==BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE)
{
powerStr += "电池健康情况:过电压" +"
";
}else if(type==BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE)
{
powerStr += "电池健康情况:未知错误" +"
";
}
text.setText(powerStr);
}
}
};
}