Get battery temperature in android programmatically

How to check battery temperature and show on screen on button click in android application in centigrade.

In this tutorial we are accessing battery temperature with the use of IntentFilter and BroadcastReceiver into centigrade form. So here is the complete step by step tutorial for Get battery temperature in android programmatically.

android-project-download-code-button

How to Get battery temperature in android programmatically.

Code for MainActivity.java file.

 package com.batterytemperature_android_examples.com;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 Button TempGet;
 TextView TempShow;
 float BatteryTemp;
 IntentFilter intentfilter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 TempGet = (Button)findViewById(R.id.button1);
 
 TempShow = (TextView)findViewById(R.id.textView1);

 intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

 
 TempGet.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 MainActivity.this.registerReceiver(broadcastreceiver,intentfilter);
 
 }
 }); 
 
 }
 
 private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 
 BatteryTemp = (float)(intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0))/10;

 TempShow.setText(BatteryTemp +" "+ (char) 0x00B0 +"C");
 }
 };
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.batterytemperature_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Temperature display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="22dp"
 android:text="Click here to Get battery temperature in android programmatically" />

</RelativeLayout>

Screenshot:

Get battery temperature in android programmatically

Click here to download Get battery temperature in android programmatically project with source code.

2 Comments

  1. its really helpful…thank you very much….

Leave a Reply

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