之前的演示程序再有在按钮按下后才去注册广播接收器,才会显示出电池电压,显然这仅仅是为了演示而已,现在我们通过改进我们的演示程序,在程序一启动就能显示出当前的电池电压.
在Activity的onCreate函数中直接注册广播接收器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//注册接收器以获取电量信息
registerReceiver(broadcastReceiver,intentFilter);
}
在OnCreate函数中注册Receiver后退出Activity时出现Receiver泄露.
06-08 12:54:11.378 11619-11619/com.example.test.testbattery E/ActivityThread: Activity com.example.test.testbattery.MainActivity has leaked IntentReceiver com.example.test.testbattery.MainActivity
1@d8e6cf8thatwasoriginallyregisteredhere.AreyoumissingacalltounregisterReceiver()?android.app.IntentReceiverLeaked:Activitycom.example.test.testbattery.MainActivityhasleakedIntentReceivercom.example.test.testbattery.MainActivity
1
@
d
8
e
6
c
f
8
t
h
a
t
w
a
s
o
r
i
g
i
n
a
l
l
y
r
e
g
i
s
t
e
r
e
d
h
e
r
e
.
A
r
e
y
o
u
m
i
s
s
i
n
g
a
c
a
l
l
t
o
u
n
r
e
g
i
s
t
e
r
R
e
c
e
i
v
e
r
(
)
?
a
n
d
r
o
i
d
.
a
p
p
.
I
n
t
e
n
t
R
e
c
e
i
v
e
r
L
e
a
k
e
d
:
A
c
t
i
v
i
t
y
c
o
m
.
e
x
a
m
p
l
e
.
t
e
s
t
.
t
e
s
t
b
a
t
t
e
r
y
.
M
a
i
n
A
c
t
i
v
i
t
y
h
a
s
l
e
a
k
e
d
I
n
t
e
n
t
R
e
c
e
i
v
e
r
c
o
m
.
e
x
a
m
p
l
e
.
t
e
s
t
.
t
e
s
t
b
a
t
t
e
r
y
.
M
a
i
n
A
c
t
i
v
i
t
y
1@d8e6cf8 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:1361)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1142)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1422)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1395)
Goole的文档说明如果是在onCreate函数中创建了receiver接收器就需要在onDestroy()函数中调用unregisterReceiver方法去取消注册.
修改代码如下:
@Override
protected void onDestroy() {
unregisterReceiver(broadcastReceiver);
}
重写OnDestory方法出现异常
06-08 12:59:23.083 15774-15774/com.example.test.testbattery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test.testbattery, PID: 15774
android.util.SuperNotCalledException: Activity {com.example.test.testbattery/com.example.test.testbattery.MainActivity} did not call through to super.onDestroy()
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4399)
解决方法:
加上super.onDestroy()代码即可解决问题。修改代码如下:
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
至此在执行演示程序也没有再出现执行异常的log了.
增减电池电压和温度的显示
定义控件
<TextView
android:id="@+id/textViewVoltage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewLevel"
android:text="" />
<TextView
android:id="@+id/textViewTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewVoltage"
android:text="" />
android:layout_below这个属性指明了该控件与另外一个控件的相对位置,这里将电压显示控件放在了电量的下方.同样的温度显示控件在电压的下方.
获取控件的id
textViewLevel = (TextView) findViewById(R.id.textViewLevel);
textViewVoltage = (TextView) findViewById(R.id.textViewVoltage);
textViewTemp = (TextView) findViewById(R.id.textViewTemp);
数值显示
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)+"%");
//获取当前的电池电压
batteryVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
textViewVoltage.setText("电压"+batteryVoltage);
//获取当前的电池温度
batteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
textViewTemp.setText("温度"+batteryTemp);
}