Android5.0 Battery开发(一)


判断是否是无线充电,USB充电,充电器充电

    public static final int BATTERY_PLUGGED_AC = 1;//充电器充电
    /** Power source is a USB port. */
    public static final int BATTERY_PLUGGED_USB = 2;//USB充电
    /** Power source is wireless. */
    public static final int BATTERY_PLUGGED_WIRELESS = 4;//无线充电
    /** @hide */
    public static final int BATTERY_PLUGGED_ANY =
            BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;


获取插入的类型

 plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

是否是无线充电

    public static boolean isWirelessCharging(int mPlugType) {
        return (mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS);
    }

是否充满

    public static boolean isBatteryFull(int level) {
        return (level >= 100);
    }

android手机的充电情况监视
/**
   * 先声明一个 IntentFilter 对象
   */
   private IntentFilter   mIntentFilter;
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.main);
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
  }
 
  protected void onResume()
  {
    super.onResume();    
    // 注册消息处理器
    registerReceiver(mIntentReceiver, mIntentFilter);
  }
 
  //声明消息处理过程
   private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
           //要看看是不是我们要处理的消息
           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {           
               //电池电量,数字
               Log.d("Battery", "" + intent.getIntExtra("level", 0));               
               //电池最大容量
               Log.d("Battery", "" + intent.getIntExtra("scale", 0));               
               //电池伏数
               Log.d("Battery", "" + intent.getIntExtra("voltage", 0));               
               //电池温度
               Log.d("Battery", "" + intent.getIntExtra("temperature", 0));
               
               //电池状态,返回是一个数字
               // BatteryManager.BATTERY_STATUS_CHARGING 表示是充电状态
               // BatteryManager.BATTERY_STATUS_DISCHARGING 放电中
               // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充电
               // BatteryManager.BATTERY_STATUS_FULL 电池满
               Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
              
               //充电类型 BatteryManager.BATTERY_PLUGGED_AC 表示是充电器,不是这个值,表示是 USB
               Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
              
               //电池健康情况,返回也是一个数字
               //BatteryManager.BATTERY_HEALTH_GOOD 良好
               //BatteryManager.BATTERY_HEALTH_OVERHEAT 过热
               //BatteryManager.BATTERY_HEALTH_DEAD 没电
               //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 过电压
               //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知错误
               Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));
           }
       }
   };


你可能感兴趣的:(Android5.0 Battery开发(一))