Подтвердить что ты не робот

Получите температуру батареи на Android

Как я могу получить температуру батареи в андроиде?

4b9b3361

Ответ 1

Попробуй это:

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

Ответ 3

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

Ответ 4

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

Ответ 6

Вы можете получить температуру процессора с помощью этой функции: Получите температуру процессора от устройства Android с помощью команды sys/class/thermal/temp.

public float getCpuTemp() {
    Process process;
    try {
        process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
        process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = reader.readLine();
        float temp = Float.parseFloat(line) / 1000.0f;
        return temp;
    } catch (Exception e) {
        e.printStackTrace();
        return 0.0f;
    }
}

В моем сообщении для отправки запросов на передачу: https://gist.github.com/sajadabasi/7d76379e82d51efd0a24e5829c3ce572