2010-10-22 126 views

回答

13

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

公共靜態最終字符串EXTRA_TEMPERATURE
額外的ACTION_BATTERY_CHANGED:包含當前的電池溫度的整數。

+0

什麼是ACTION_BATTERY_CHANGED呢?我必須午餐才能獲得溫度嗎?我是否需要聽系統廣泛的意圖? – Christian 2010-10-22 13:59:19

+0

谷歌是你的朋友:http://www.tutorialforandroid.com/2009/01/getting-battery-information-on-android.html該教程是關於如何獲得電池電量,但與我發佈的文檔鏈接,我相信你可以弄清楚如何獲得電池溫度,只需使用「溫度」而不是「電平」... – Select0r 2010-10-22 14:26:10

+10

@Christian:如果你不想要註冊一個實際的'BroadcastReceiver'至。調用registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED))',它將返回爲此操作廣播的最後一個Intent。使用額外的'EXTRA_TEMPERATURE'來獲得你想要的值。 – CommonsWare 2010-10-22 14:38:31

1

嘗試讀取靜態INT BatteryManager.EXTRA_TEMPERATURE

+0

根據文檔EXTRA_TEMPERATURE碰巧是一個字符串。它也是靜態的,因此將永遠是「溫度」。 – Christian 2010-10-22 14:11:45

+0

確實如此,但文檔還說:「Extra for ACTION_BATTERY_CHANGED」 - 因此,您的靜態字符串只是從ACTION_BATTERY_CHANGED獲取溫度的關鍵。 – Select0r 2010-10-22 14:28:36

1
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

試試這個:

private mBatInfoReceiver myBatInfoReceiver; 

和的onCreate:

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

    } 

    }; 

然後在您的變量聲明定義

@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(調用e.g)

float temp = myBatInfoReceiver.get_temp(); 

String message = "Current " + BatteryManager.EXTRA_TEMPERATURE + " = " + 
        temp + Character.toString ((char) 176) + " C"; 
4
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"; 
    } 
0

您可以通過此功能讓CPU溫度: 使用sys/class/thermal/temp命令獲取來自Android設備的CPU溫度。

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