电池温度返回null

时间:2012-07-09 17:52:20

标签: android android-layout android-intent

我有问题。 :)这是一个小小的,但如果有人可以提供帮助,我将不胜感激!

事情是我想拿取Android设备的电池信息,但它返回null。我已经做了一切正确但无法弄清楚错误。 :(

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources_battery);

    BroadcastReceiver batteryData = new BroadcastReceiver() {
        String tech,temp;
        int level;
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            tech = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
            temp = intent.getStringExtra(BatteryManager.EXTRA_TEMPERATURE);

            TextView technology = (TextView) findViewById(R.id.Appres_BatType);
            technology.setText("Current Technology : " + tech);
            TextView Tempreture = (TextView) findViewById(R.id.Appres_Battemp);
            Tempreture.setText("Current Battery Tempreature : " +temp);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryData, filter);
}

上面是我为了获得temp而编写的代码,但它返回null! 请帮忙!

我将代码更改为此bt仍然返回零!

temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);

2 个答案:

答案 0 :(得分:6)

temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);

是获得温度的正确方法。

这对我有用:

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resources_battery);

BroadcastReceiver batteryData = new BroadcastReceiver() {
    String tech;
    int level,temp;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        tech = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
        level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
        temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);

        TextView technology = (TextView) findViewById(R.id.Appres_BatType);
        technology.setText("Current Technology : " + tech);
        TextView Tempreture = (TextView) findViewById(R.id.Appres_Battemp);
        Tempreture.setText("Current Battery Tempreature : " +temp);
    }
};

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryData, filter);
}

也许您碰巧有一台设备无法正确报告电池温度?我的设备(Galaxy Nexus)报告:330(在1/10摄氏度,请参阅this问题)。

答案 1 :(得分:2)

您只需将temp更改为int并使用您编写的最后一行

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources_battery);

    BroadcastReceiver batteryData = new BroadcastReceiver() {
    String tech;
    int level,temp;
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            tech = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,-1);

            TextView technology = (TextView) findViewById(R.id.Appres_BatType);
            technology.setText("Current Technology : " + tech);
            TextView Tempreture = (TextView) findViewById(R.id.Appres_Battemp);
            Tempreture.setText("Current Battery Tempreature : " +temp);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryData, filter);
}