When you call registerReceiver, specifying an Intent Filter that matches a 
sticky Broadcast Intent, the return value will be the last Intent 
broadcast, such as the battery-level changed broadcast:
 

IntentFilter battery = new IntentFilter( Intent.ACTION_BATTERY_CHANGED);
Intent currentBatteryCharge = registerReceiver( null, battery); 

As shown in the preceding snippet, it is not necessary to specify a 
Receiver to obtain the current value of a sticky Intent. As a result, many 
of the system device state broadcasts (such as battery and docking state) 
use sticky Intents to improve efficiency. These are examined in more detail 
later in this chapter. To broadcast your own sticky Intents, your 
application must have the BROADCAST_STICKY uses-permission before calling 
sendStickyBroadcast and passing in the relevant Intent: 
sendStickyBroadcast( intent); To remove a sticky Intent, call 
removeStickyBroadcast, passing in the sticky Intent to remove: 
removeStickyBroadcast( intent);

Meier, Reto (2012-04-05). Professional Android 4 Application Development 
(Wrox Professional Guides) (Kindle Locations 4825-4838). John Wiley and 
Sons. Kindle Edition. 



On Tuesday, January 1, 2013 12:23:17 AM UTC-6, Jiggy wrote:
>
> Hi there,
>
>
> I would like to get the battery consumption for sending an SMS or 
> downloading data (~1MB) from server. 
>
>
> Currently, I am using following way to get the information about battery 
> voltage, level and etc. 
>
>
>
>
> private void getBatLevel(){ 
>
> BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() { 
>
> public void onReceive(Context context, Intent intent) { 
>
> int scale = -1; 
>
> int level = -1; 
>
> int voltage = -1; 
>
> int temp = -1; 
>
> context.unregisterReceiver(this); 
>
> level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
>
> scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 
>
> temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); 
>
> voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); 
>
> batUsageString.setText(String.valueOf(voltage)); 
>
> Log.d("DEBUG", "level is "+level+"/"+scale+", temp is "+temp+", voltage is 
> "+voltage); 
>
> } 
>
> }; 
>
> IntentFilter batteryLevelFilter = new 
> IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
>
> registerReceiver(batteryLevelReceiver, batteryLevelFilter); 
>
> }
>
>
>
>
> I would like to know if there is any other way to get the precise 
> information about battery (I mean in float or double)
>
>
>
>
> PS: I am calling above method before and after sending an SMS.
>
> Thanks
>
>
>
>
> Regards
>
> Jiggy
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to