在Android上获取电池温度 [英] Get temperature of battery on android

查看:61
本文介绍了在Android上获取电池温度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在android中获取电池的温度?

How do I get the temperature of the battery in android?

推荐答案

试试这个:

private class mBatInfoReceiver extends BroadcastReceiver{ 

    int temp = 0;

    float get_temp(){
        return (float)(temp / 10);
    }

    @Override 
    public void onReceive(Context arg0, Intent intent) {
        temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
    }

};

然后在你的变量声明中定义:

then define in your Variable declarations:

private mBatInfoReceiver myBatInfoReceiver;

并在 onCreate 中:

and in onCreate:

    @Override 
    public void onCreate(Bundle b) { 
        super.onCreate(b);  
        setContentView(R.layout.activity_main);

        // ...
        // Add this

        myBatInfoReceiver = new mBatInfoReceiver();                                     

        this.registerReceiver(this.myBatInfoReceiver,
                              new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

     } 

稍后调用,例如在 OnClickListener() 中

later call e.g in a OnClickListener()

float temp = myBatInfoReceiver.get_temp(); 

String message = "Current " + BatteryManager.EXTRA_TEMPERATURE + " = " +
                  temp +  Character.toString ((char) 176) + " C";

这篇关于在Android上获取电池温度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆