Reputation: 1288
I am trying to make a Battery Alarm Application. I was reading the Android docs and found out that we don't need to register a BroadcastReceiver. We could do something like this-
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
MY QUESTIONS ARE-
Upvotes: 2
Views: 1781
Reputation: 389
Q1. Yes, system provide every time .
Example :
private class BatteryBroadcastReceiver extends BroadcastReceiver {
private final static String BATTERY_LEVEL = "level";
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BATTERY_LEVEL, 0);
mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level);
mBatteryLevelProgress.setProgress(level);
}
}
//Register our BroadcastReceiver in application
BatteryBroadcastReceiver mReceiver = new BatteryBroadcastReceiver();
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
//Update your progress bar get
public class MainActivity extends Activity {
private TextView mBatteryLevelText;
private ProgressBar mBatteryLevelProgress;
private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBatteryLevelText = (TextView) findViewById(R.id.textView);
mBatteryLevelProgress = (ProgressBar) findViewById(R.id.progressBar);
mReceiver = new BatteryBroadcastReceiver();
}
@Override
protected void onStart() {
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onStart();
}
@Override
protected void onStop() {
unregisterReceiver(mReceiver);
super.onStop();
}
private class BatteryBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level);
mBatteryLevelProgress.setProgress(level);
if(level<10){
// play Battery alarm.
}
}
}
Upvotes: 0
Reputation: 1092
Create receiver for getting information.. This is better way.
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
In Manifest add
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
</intent-filter>
</receiver>
For more information Visit LINK
Upvotes: 0
Reputation: 17131
First add intent filter in manifest file:
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
After add new class in project:
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
For get battery level:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
More info this link : developer.android
Upvotes: 0
Reputation: 3914
here is a full sample to get battery level
private float getBatteryLevel() {
Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int batteryLevel = -1;
int batteryScale = 1;
if (batteryStatus != null) {
batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
}
return batteryLevel / (float) batteryScale * 100;
}
answering your questions:
1- Yes, you can get additional information by getExtra
and define the key,BatteryManager
has a lot of keys to extract information from the intent.
2- no, you just have to register the reciever and unregister it accoording to the Activity or Fragment lifcycle, usually registering in onCreate()/onCreateView()
and unregistering in onDestry()/onDestroyView()
.
3- you can create a bound service that has a simple notification interface. you register your reciever on it, and from the previous sample you could do something like this:
if (getBatteryLevel < 10) {
//show notification or do whatever you want.
}
Upvotes: 2