Skip to content

Commit 23dc3ec

Browse files
draft battery management
1 parent 50aa77a commit 23dc3ec

File tree

1 file changed

+22
-4
lines changed
  • app/src/main/java/net/opencurlybraces/android/projects/androidpractices

1 file changed

+22
-4
lines changed

app/src/main/java/net/opencurlybraces/android/projects/androidpractices/MainActivity.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package net.opencurlybraces.android.projects.androidpractices;
22

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;
76
import android.os.Bundle;
7+
import android.support.v7.app.ActionBarActivity;
88
import android.view.Menu;
99
import android.view.MenuItem;
1010
import android.widget.TextView;
1111

12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
1214
import butterknife.ButterKnife;
1315
import butterknife.InjectView;
1416

@@ -18,6 +20,7 @@ public class MainActivity extends ActionBarActivity {
1820

1921
@InjectView (R.id.hello_text)
2022
TextView mHelloText;
23+
AtomicBoolean mIsCharging = new AtomicBoolean();
2124

2225
@Override
2326
protected void onCreate(Bundle savedInstanceState) {
@@ -31,6 +34,21 @@ protected void onCreate(Bundle savedInstanceState) {
3134
}
3235

3336

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+
3452
@Override
3553
public boolean onCreateOptionsMenu(Menu menu) {
3654
// Inflate the menu; this adds items to the action bar if it is present.

0 commit comments

Comments
 (0)