您的位置:首页 > 移动开发 > Android开发

Android - 电池状态

2015-08-16 12:25 513 查看
为了解决电池图标的问题,顺带看了看电池信息的获取方法 ;自己写了一个小栗子,来验证一下效果

电池的信息,一般都在BatteryManager里面,信息是用广播发出的。我们更新信息需要一个广播接收器

注册一个广播接收器,接收 Intent.ACTION_BATTERY_CHANGED ,从intent中读出想要的电池信息

比如 BatteryManager.EXTRA_STATUS BatteryManager.BATTERY_STATUS_CHARGING 等等

在Android 5.1中,电池信息可以在Settings - Battery 里面找到

package com.example.chargingwatching;

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.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView chargeStatus;
private TextView LevelDigit;
private TextView percentView;
private int rustLevel = 0;    /* battery level */
private int windowWidth = 0;
private WindowManager mWindowManager;

private BroadcastReceiver mBatteryStatuReceiver = new BroadcastReceiver() {
int status;    // current battery status
int plugType;
public void onReceive(Context context, Intent intent)    {

rustLevel = (int)(100f
* intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
/ intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));

LevelDigit.setText("  " + rustLevel + "%");

percentView.setWidth((int)(windowWidth * rustLevel /100));
status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

if(status == BatteryManager.BATTERY_STATUS_CHARGING) {
if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
chargeStatus.setText(R.string.ac_charging);
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
chargeStatus.setText(R.string.usb_charging);
}
} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
chargeStatus.setText(R.string.full_ac);
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
chargeStatus.setText(R.string.full_usb);
}
} else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
chargeStatus.setText(R.string.uncharge);
} else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
chargeStatus.setText(R.string.discharging);
} else {
chargeStatus.setText(R.string.unknown);
}
}
};

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_charging);
chargeStatus = (TextView)findViewById(R.id.tv_charge);
LevelDigit = (TextView) findViewById(R.id.tv_battery_level_digit);
percentView = (TextView) findViewById(R.id.percent_view);

mWindowManager = this.getWindowManager();
windowWidth = mWindowManager.getDefaultDisplay().getWidth();

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBatteryStatuReceiver, filter);
}
}


以下是简单的布局文件

主体是LinearLayout,放一个TextView来显示电池状态,一个TextView来显示电量百分比

一个简陋的电量条,拿TextView来冒充的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_charge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Detecting..."
android:textSize="20sp"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_battery_level"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/battery_level"
android:textSize="20sp"/>

<TextView
android:id="@+id/tv_battery_level_digit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"/>
</LinearLayout>

<TextView
android:id="@+id/percent_view"
android:layout_height="20dp"
android:layout_width="wrap_content"
android:background="#00AA00"
/>
</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: