通过一个显示电池电量的小程序来学习了解android的基本开发
如上图一个文本框用于显示电池电量,一个按钮当点击按钮后可以立即显示当前的电池电量.
定义需要的控件
在activity_main.xml中定义我们需要的控件信息:文本框TextView和按钮Button。
代码如下:
<TextView
android:id="@+id/textViewBattery"
android:text="Battery Scale:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonShowBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showBattery"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"></Button>
在MainActivity.java文件的onCreate函数中获得控件的id
buttonShowBattery= (Button) findViewById(R.id.buttonShowBattery);
textViewLevel = (TextView) findViewById(R.id.textViewBattery);
编写按钮的监听函数
在按钮的监听函数中注册广播接收器。
buttonShowBattery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//注册接收器以获取电量信息
registerReceiver(broadcastReceiver,intentFilter);
}
});
编写广播接收器函数用于接收系统广播
private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//获取当前电量,如未获取具体数值,则默认为0
batteryLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
//获取最大电量,如未获取到具体数值,则默认为100
batteryScale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,100);
//显示电量
textViewLevel.setText("电量"+(batteryLevel*100/batteryScale)+"%");
}
当电量变化时系统就会发出ACTION_BATTERY_CHANGED的广播,broadcastReceiver用于处理接收到的广播信息显示出当前的电池电量。
完整代码
MainActivity.java
package com.example.test.testbattery;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewLevel=null;
private Button buttonShowBattery;
private int batteryLevel;
private int batteryScale;
private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//获取当前电量,如未获取具体数值,则默认为0
batteryLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
//获取最大电量,如未获取到具体数值,则默认为100
batteryScale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,100);
//显示电量
textViewLevel.setText("电量"+(batteryLevel*100/batteryScale)+"%");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonShowBattery= (Button) findViewById(R.id.buttonShowBattery);
textViewLevel = (TextView) findViewById(R.id.textViewBattery);
buttonShowBattery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//注册接收器以获取电量信息
registerReceiver(broadcastReceiver,intentFilter);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.test.testbattery.MainActivity">
<TextView
android:id="@+id/textViewBattery"
android:text="Battery Scale:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonShowBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showBattery"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"></Button>
</RelativeLayout>