Get battery temperature in android programmatically

Sometimes when you are developing an android application, you may want to get current batter temperature
For example:
Lets say if battery temperature is too high, you may want to close some application.
In this post, we are going to see how to get current battery temperature in android.

Source code:

Download

click to begin
20KB .zip

Step 1 :

Create an android application project named “GetBatteryTemperatureApp”.

Step 2 :

Change res ->layout -> activity_main.xml as below:

Step 3:

Change src/main/packageName/MainActivity.java as below:

As we want to get battery level change information, we need to register broadcast receiver and intent filter. An intent filter is used to tell native battery app that this app is listening for changes in native battery app.
Lets understand above code:

Intent.ACTION_BATTERY_CHANGED:

If you see the code we have used Intent.ACTION_BATTERY_CHANGED to get current battery info. It is sticky broadcast which contain battery charging state , level and other info. BatteryManager class contains all constant strings.
Sticky broadcast is broadcast which will remain active once registered.

Here we are registering created broadcastreceiver to the MainActivity and intent filter is listening for battery change events.

Run the app:

When you run above app on actual device, you will get below screen :

When you click on button, you will get below screen :

Related Posts

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

  • Convert PDF to text in Python
    07 January

    Convert PDF to Text in Python

    Table of ContentsConvert pdf to text using pypdf2Convert pdf to text using PDFminerConclusion Python is a feature-rich programming language. We can perform different operations on files in python using the different modules and libraries. In this article, we will discuss different ways to convert a pdf file to text in python. Convert pdf to text […]

  • Initialize empty array in java
    01 January

    Initialize empty array in java

    Table of ContentsIntroductionHow do you initialize an empty array in java?Using new Keyword with predefined Values and SizeUsing Anonymous Array Objects to initialize empty arrayUsing java.util.Scanner Class for user input with predefined size. Using java.io.BufferedReader to initialize array for user input with unknown sizeUsing fill() method of java.util.Arrays Class to initialize empty array 💡 Outline You […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusion In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how we can append […]

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Python range float
    17 December

    Generate float range in Python [9 ways]

    Table of ContentsWhat is a floating-point number in Python?Why not use the simple and popular range() function?Python range float : How to generate floating-point numbers in Python?Using the numpy.linspace() method.Using the numpy.arange() method.Using list comprehension.Using generator comprehension.Using yield generator.Using a user-defined function.Using the itertools module and its functions.Using the map() function along with a lambda […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.