battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 Android Studio alchemy series : get device temprature

Go down 
AuthorMessage
Moti Barski
super
super
Moti Barski

Posts : 562
Join date : 2011-08-02

Android Studio alchemy series : get device temprature Empty
PostSubject: Android Studio alchemy series : get device temprature   Android Studio alchemy series : get device temprature EmptyThu Feb 25, 2021 2:13 am

this series aims to add varies app physical abilities with as little code lines inside the MainActivity. it is what the android studio IDEs should include as a default but they don't
so thank your lucky star I did it.

Code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.widget.Toast;

public class TempratureUtil {
    /*
    * this class gets the battery power level
    * init (in kotlin main class): val tempratureUtil = TempratureUtil();//battery level getting util //glabal var
    * step 2 : register reciever : this.registerReceiver(tempratureUtil.broadcastreceiver,tempratureUtil.intentfilter);
    * to get the level of the power use : tempratureUtil.b8TriTemp
    * */
    public IntentFilter intentfilter= new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    private float batteryTemp;
    public BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //gets called every 2 seconds
            batteryTemp = (float)(intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0))/10;
        }
    };

    public float getBatteryTemp() {
        return batteryTemp;
    }
    public String getB8TriTemp() {
        return batteryTemp+"";
    }
}

read the class documentation

ryu ranger

_________________
MB over and out emp it up
Back to top Go down
 
Android Studio alchemy series : get device temprature
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: the LivinGrimoire-
Jump to: