Skip to content

Commit e4fe192

Browse files
Receiver declared in AndroidManifest.xml.
PowerUtils.java added along with the isBatteryCharging() Method.
1 parent a9d7e18 commit e4fe192

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="net.opencurlybraces.android.projects.androidpractices" >
3+
package="net.opencurlybraces.android.projects.androidpractices">
44

55
<application
66
android:allowBackup="true"
77
android:icon="@drawable/ic_launcher"
88
android:label="@string/app_name"
9-
android:theme="@style/AppTheme" >
9+
android:theme="@style/AppTheme">
1010
<activity
1111
android:name=".MainActivity"
12-
android:label="@string/app_name" >
12+
android:label="@string/app_name">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
<!-- Battery charging status receiver -->
20+
<receiver android:name=".receiver.PowerConnectionReceiver">
21+
<intent-filter>
22+
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
23+
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
24+
</intent-filter>
25+
</receiver>
1926
</application>
2027

2128
</manifest>

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

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

3+
import android.content.Context;
34
import android.content.Intent;
45
import android.content.IntentFilter;
56
import android.os.BatteryManager;
@@ -9,6 +10,7 @@
910
import android.view.MenuItem;
1011
import android.widget.TextView;
1112

13+
import net.opencurlybraces.android.projects.androidpractices.util.PowerUtils;
1214
import net.opencurlybraces.android.projects.androidpractices.util.PrefUtils;
1315

1416
import java.util.concurrent.atomic.AtomicBoolean;
@@ -22,7 +24,7 @@ public class MainActivity extends ActionBarActivity {
2224

2325
@InjectView (R.id.hello_text)
2426
TextView mHelloText;
25-
AtomicBoolean mIsCharging = new AtomicBoolean();
27+
2628

2729
@Override
2830
protected void onCreate(Bundle savedInstanceState) {
@@ -40,17 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {
4042
protected void onResume() {
4143
super.onResume();
4244

43-
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
44-
Intent batteryStatus = MainActivity.this.registerReceiver(null, ifilter);
45-
46-
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
47-
boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING || status ==
48-
BatteryManager.BATTERY_STATUS_FULL);
45+
PrefUtils.setBatteryCharging(this, PowerUtils.isBatteryCharging(this));
4946

50-
if ((status == BatteryManager.BATTERY_STATUS_CHARGING) ^ mIsCharging.get()) {
51-
mIsCharging.set(isCharging);
52-
PrefUtils.setBatteryCharging(this, isCharging);
53-
}
5447
}
5548

5649

@@ -132,4 +125,10 @@ public void onTrimMemory(int level) {
132125
}
133126
}
134127

128+
/**
129+
* Return the battery charging status
130+
*
131+
* @return boolean
132+
*/
133+
135134
}

app/src/main/java/net/opencurlybraces/android/projects/androidpractices/receiver/PowerConnectionReceiver.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
import android.content.BroadcastReceiver;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.os.BatteryManager;
7+
8+
import net.opencurlybraces.android.projects.androidpractices.util.PrefUtils;
69

710
/**
811
* Receiver thats listen to changes in power status changes
912
* <p/>
10-
* TODO implement and define in manifest
11-
* Created by chris on 16/01/15.
13+
* TODO implement and define in manifest Created by chris on 16/01/15.
1214
*/
1315
public class PowerConnectionReceiver extends BroadcastReceiver {
1416
@Override
1517
public void onReceive(Context context, Intent intent) {
1618

19+
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
20+
boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING || status ==
21+
BatteryManager.BATTERY_STATUS_FULL);
22+
23+
PrefUtils.setBatteryCharging(context, isCharging);
1724
}
1825
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.opencurlybraces.android.projects.androidpractices.util;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.content.IntentFilter;
6+
import android.os.BatteryManager;
7+
8+
/**
9+
* Created by chris on 16/01/15.
10+
*/
11+
public class PowerUtils {
12+
13+
14+
/**
15+
* Try to get the battery charging status using the sticky intents {@code android.intent.action
16+
* .ACTION_POWER_CONNECTED} and {@code android.intent.action .ACTION_POWER_DISCONNECTED} .
17+
* If there's no sticky intent, get the value from {@link android.content .SharedPreferences}
18+
*
19+
* @param context
20+
* @return boolean
21+
*/
22+
public static boolean isBatteryCharging(final Context context) {
23+
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
24+
Intent batteryStatus = context.registerReceiver(null,
25+
ifilter); //Get a sticky intent or null
26+
27+
if (batteryStatus == null) return PrefUtils.isBatteryCharging(context);
28+
29+
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
30+
31+
return (status == BatteryManager.BATTERY_STATUS_CHARGING || status ==
32+
BatteryManager.BATTERY_STATUS_FULL);
33+
34+
}
35+
}

app/src/main/java/net/opencurlybraces/android/projects/androidpractices/util/PrefUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static boolean isBatteryCharging(final Context context) {
3232
}
3333

3434
/**
35-
* Stores asynchronously the battery charging status in {@link SharedPreferences}.
35+
* Store asynchronously the battery charging status in {@link SharedPreferences}.
3636
*
3737
* @param context
3838
* @param isCharging

0 commit comments

Comments
 (0)