可能重复:
在android上获取电池温度
我是安卓新手,正在制作一个显示安卓电池温度的应用程序每 10 秒更新一次……请帮帮我
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// setContentView(R.layout.activity_blue_tooth);
LinearLayout big = new LinearLayout(this);
big.setOrientation(LinearLayout.VERTICAL);
LinearLayout small[]= new LinearLayout[5];
for(int i=0; i<5; i++)
{
small[i]= new LinearLayout(this);
small[i].setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linearparams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
temp = new TextView(this);
small[i].addView(temp);
volt= new TextView(this);
small[i].addView(volt);
big.addView(small[i]);
}
setContentView(big);
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
temp.setText("Temperature="+String.valueOf(temperature) + "%");
volt.setText(" " +"Voltage="+String.valueOf(voltage) + "%");
}
};
我正在获取电池温度和电压,但我无法每 10 秒更新一次