在android上获取电池温度

发布于 2024-09-28 11:49:17 字数 25 浏览 4 评论 0原文

android 如何获取电池的温度?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哆兒滾 2024-10-05 11:49:17

http://developer.android.com/reference/android/os/BatteryManager.html

公共静态最终字符串 EXTRA_TEMPERATURE
ACTION_BATTERY_CHANGED 的额外内容:包含当前电池温度的整数。

http://developer.android.com/reference/android/os/BatteryManager.html

public static final String EXTRA_TEMPERATURE
Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.

徒留西风 2024-10-05 11:49:17

试试这个:

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);
    }

};

然后在变量声明中定义:

private mBatInfoReceiver myBatInfoReceiver;

并在 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() 中调用

float temp = myBatInfoReceiver.get_temp(); 

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

Try this:

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;

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));

     } 

later call e.g in a OnClickListener()

float temp = myBatInfoReceiver.get_temp(); 

String message = "Current " + BatteryManager.EXTRA_TEMPERATURE + " = " +
                  temp +  Character.toString ((char) 176) + " C";
少女情怀诗 2024-10-05 11:49:17
    public static String batteryTemperature(Context context)
    {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));    
        float  temp   = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)) / 10;
        return String.valueOf(temp) + "*C";
    }
    public static String batteryTemperature(Context context)
    {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));    
        float  temp   = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)) / 10;
        return String.valueOf(temp) + "*C";
    }
故事↓在人 2024-10-05 11:49:17
TextView BatTemp;

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent intent) 
        {

          // TODO Auto-generated method stub

          int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);

      };

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


        BatTemp = (TextView) this.findViewById(R.id.textView8);

        this.registerReceiver(this.mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
      }
TextView BatTemp;

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent intent) 
        {

          // TODO Auto-generated method stub

          int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);

      };

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


        BatTemp = (TextView) this.findViewById(R.id.textView8);

        this.registerReceiver(this.mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
      }
木落 2024-10-05 11:49:17

尝试读取静态 int BatteryManager.EXTRA_TEMPERATURE

Try reading the static int BatteryManager.EXTRA_TEMPERATURE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文