android 电源管理

来源:互联网 发布:人工智能答案 编辑:程序博客网 时间:2024/04/29 17:42


一、在AndroidManifest.xml中使用电源管理权限

<uses-permission 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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textStateId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电量状态:" />

</LinearLayout>


二、AndroidMainfest.xml  添加了2个获取电量状态权限


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.powertest"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DEVICE_POWER" />


    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.powertest.Power"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


三、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)+"\n";
   //电池最大容量
   powerStr += "最大电池电量" + intent.getIntExtra("scale", 0)+"\n";
   //电池伏数
   powerStr += "电池伏数" + intent.getIntExtra("voltage", 0)+"\n";
   //电池温度
   powerStr += "电池温度" + intent.getIntExtra("temperature", 0)+"\n"; 


   type = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
   if(type==BatteryManager.BATTERY_STATUS_CHARGING)
   {
    powerStr += "电池状态:" + "充电状态"+"\n"; 
   }else if(type==BatteryManager.BATTERY_STATUS_DISCHARGING)
   {
    powerStr += "电池状态:" + "放电中"+"\n"; 
   }else if(type==BatteryManager.BATTERY_STATUS_NOT_CHARGING)
   {
    powerStr += "电池状态:" + "未充电"+"\n"; 
   }else if(type==BatteryManager.BATTERY_STATUS_FULL)
   {
    powerStr += "电池状态:" + "电池满"+"\n"; 
   }    
   type=intent.getIntExtra("plugged", 0);
   Log.e("Battery", "" + intent.getIntExtra("plugged", 0));
   if(BatteryManager.BATTERY_PLUGGED_AC==type)
   {
    powerStr += "充电类型:充电器" +"\n";
   }else{
    powerStr += "充电类型:usb" +"\n";
   }
   type=intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
   if(type==BatteryManager.BATTERY_HEALTH_GOOD)
   {
    powerStr += "电池健康情况:良好" +"\n";
   }else if(type==BatteryManager.BATTERY_HEALTH_OVERHEAT)
   {
    powerStr += "电池健康情况:过热" +"\n";
   }else if(type==BatteryManager.BATTERY_HEALTH_DEAD)
   {
    powerStr += "电池健康情况:没电" +"\n";
   }else if(type==BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE)
   {
    powerStr += "电池健康情况:过电压" +"\n";
   }else if(type==BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE)
   {
    powerStr += "电池健康情况:未知错误" +"\n";
   }
   text.setText(powerStr);
  }
 }
};
}



0 0
原创粉丝点击