1
1
package net .opencurlybraces .android .projects .androidpractices ;
2
2
3
- import android .annotation .TargetApi ;
4
- import android .content .ComponentCallbacks2 ;
5
- import android .os .Build ;
6
- import android .support .v7 .app .ActionBarActivity ;
3
+ import android .content .Intent ;
4
+ import android .content .IntentFilter ;
5
+ import android .os .BatteryManager ;
7
6
import android .os .Bundle ;
7
+ import android .support .v7 .app .ActionBarActivity ;
8
8
import android .view .Menu ;
9
9
import android .view .MenuItem ;
10
10
import android .widget .TextView ;
11
11
12
+ import java .util .concurrent .atomic .AtomicBoolean ;
13
+
12
14
import butterknife .ButterKnife ;
13
15
import butterknife .InjectView ;
14
16
@@ -18,6 +20,7 @@ public class MainActivity extends ActionBarActivity {
18
20
19
21
@ InjectView (R .id .hello_text )
20
22
TextView mHelloText ;
23
+ AtomicBoolean mIsCharging = new AtomicBoolean ();
21
24
22
25
@ Override
23
26
protected void onCreate (Bundle savedInstanceState ) {
@@ -31,6 +34,21 @@ protected void onCreate(Bundle savedInstanceState) {
31
34
}
32
35
33
36
37
+ @ Override
38
+ protected void onResume () {
39
+ super .onResume ();
40
+
41
+ IntentFilter ifilter = new IntentFilter (Intent .ACTION_BATTERY_CHANGED );
42
+ Intent batteryStatus = MainActivity .this .registerReceiver (null , ifilter );
43
+
44
+ int status = batteryStatus .getIntExtra (BatteryManager .EXTRA_STATUS , -1 );
45
+ if ((status == BatteryManager .BATTERY_STATUS_CHARGING ) != mIsCharging .get ()) {
46
+ mIsCharging .set (status == BatteryManager .BATTERY_STATUS_CHARGING );
47
+
48
+ }
49
+ }
50
+
51
+
34
52
@ Override
35
53
public boolean onCreateOptionsMenu (Menu menu ) {
36
54
// Inflate the menu; this adds items to the action bar if it is present.
0 commit comments