Read Battery Level?

Put your problem here if it does not fit any of the other categories.

Read Battery Level?

Postby The Spirit » Sun Jan 04, 2009 8:54 pm

HI.
Is there something to read the actual battery level.
Searched through the web and in the android groups, but i couldn´find any solution how to get the battery level.
Does someone know, how to get it?
Thanks
The Spirit
Developer
Developer
 
Posts: 48
Joined: Fri Jan 02, 2009 1:59 pm
Location: Germany

Top

Postby tum0rc0re » Mon Jan 05, 2009 7:13 am

Here :)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package maximyudin.BatteryLevel;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.BroadcastReceiver;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.content.Intent;
  12.  
  13. import android.content.IntentFilter;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.widget.TextView;
  18.  
  19.  
  20.  
  21. public class BatteryLevel extends Activity {
  22.  
  23.         private TextView tvBatteryLevel;
  24.  
  25.        
  26.  
  27.         private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
  28.  
  29.                 @Override
  30.  
  31.                 public void onReceive(Context context, Intent intent) {
  32.  
  33.                         String action = intent.getAction();
  34.  
  35.                         if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
  36.  
  37.  
  38.  
  39.                                 int level = intent.getIntExtra("level", 0);
  40.  
  41.                                 int scale = intent.getIntExtra("scale", 100);
  42.  
  43.  
  44.  
  45.                                 tvBatteryLevel.setText("Battery level: "
  46.  
  47.                                                 + String.valueOf(level * 100 / scale) + "%");
  48.  
  49.                         }
  50.  
  51.                 }
  52.  
  53.         };
  54.  
  55.        
  56.  
  57.         @Override
  58.  
  59.         public void onResume() {
  60.  
  61.                 super.onResume();
  62.  
  63.  
  64.  
  65.                 registerReceiver(mBatteryInfoReceiver, new IntentFilter(
  66.  
  67.                                 Intent.ACTION_BATTERY_CHANGED));
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         @Override
  74.  
  75.         public void onPause() {
  76.  
  77.                 super.onPause();
  78.  
  79.  
  80.  
  81.                 unregisterReceiver(mBatteryInfoReceiver);              
  82.  
  83.         }
  84.  
  85.  
  86.  
  87.     @Override
  88.  
  89.     public void onCreate(Bundle savedInstanceState) {
  90.  
  91.         super.onCreate(savedInstanceState);
  92.  
  93.         setContentView(R.layout.main);
  94.  
  95.        
  96.  
  97.         tvBatteryLevel = (TextView) findViewById(R.id.tvBatteryLevel);
  98.  
  99.     }
  100.  
  101. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13. <TextView  
  14.  
  15.    android:layout_width="fill_parent"
  16.  
  17.    android:layout_height="wrap_content"
  18.  
  19.    android:id="@+id/tvBatteryLevel" android:text="Battery level:"/>
  20.  
  21. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby The Spirit » Mon Jan 05, 2009 12:19 pm

thanks.
managed it today morning at around 2 o´clock myself.
done it a little bit different.
and i think you don´t need "scale".
works proper without it
The Spirit
Developer
Developer
 
Posts: 48
Joined: Fri Jan 02, 2009 1:59 pm
Location: Germany

Postby tum0rc0re » Mon Jan 05, 2009 12:34 pm

I think that it needs, in source of android so code, i don't think that google is not right
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby The Spirit » Mon Jan 05, 2009 12:37 pm

i can only say, that i tried it, and it works fine.
have to get a deeper look, what "scale" is doing
The Spirit
Developer
Developer
 
Posts: 48
Joined: Fri Jan 02, 2009 1:59 pm
Location: Germany

Postby jonneymendoza » Thu Oct 22, 2009 3:06 pm

Hi, i have a quick qestion about this. i am trying to understand the code and the only bit i do not understand is how you knew what the extended data was for the ACTION_BATTERY_CHANGE intent was :

int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);


I could not find any information about this in the docs so am wondering how you know "level" and "scale" would work as the only thing remotely connected to ACTION_BATTERY_CHANGE is the BatteryManager class which has no level or scale methods.
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Top

Postby jonneymendoza » Fri Oct 23, 2009 10:19 am

Hi, i have a quick qestion about this. i am trying to understand the code and the only bit i do not understand is how you knew what the extended data was for the ACTION_BATTERY_CHANGE intent was :

int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);


I could not find any information about this in the docs so am wondering how you know "level" and "scale" would work as the only thing remotely connected to ACTION_BATTERY_CHANGE is the BatteryManager class which has no level or scale methods.
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Postby peter.taylor » Sun Feb 14, 2010 10:33 pm

is there a similar bit of code to get the temperature of the battery too?
peter.taylor
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 14, 2010 10:25 pm

Re: Read Battery Level?

Postby muslidrikk » Tue Jun 01, 2010 12:20 pm

Hello,

If I understand the code correctly, you'll be able to read battery level only if you receive an intent with action ACTION_BATTERY_CHANGED.
But what if you want to monitor regularly, each minute for instance, the battery level? You won't get a new intent ACTION_BATTERY_CHANGED each minute, so you won't be able to retrieve the battery level information from there?

/Muslidrikk
muslidrikk
Freshman
Freshman
 
Posts: 6
Joined: Wed May 19, 2010 7:53 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests