1

I have a problem. :) It's a tiny one but appreciate if some one can help!

the thing is I take want to take the battery information of the android device but it returns me null. I have done everything correct bt cant figure out the error. :(

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

Above is the code which i wrote to get the temp but it returns null! Please help!

I changed the code to this bt still it returns zero!

temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
Imesh Chandrasiri
  • 5,558
  • 15
  • 60
  • 103

2 Answers2

6

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

is the correct way to get the temperature.

This works for me:

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

Maybe you happen to have a device that doesn't correctly report the battery temperature? My device (Galaxy Nexus) reports: 330 (in 1/10 of a Centigrade, see this question).

Community
  • 1
  • 1
Nick
  • 3,504
  • 2
  • 39
  • 78
  • I'm testing the app on an AVD.. I'm new to android development nd I hope the AVD contains battery information as such! – Imesh Chandrasiri Jul 09 '12 at 18:14
  • An AVD will return 0 (instead of `Null`, if you use the above code). A real device will return the actual value (e.g. 330 on my phone). – Nick Jul 09 '12 at 18:29
  • thanks mate! if I dint ask this, I would've been thinking how to figure this out all nyt! :) thanks again! – Imesh Chandrasiri Jul 09 '12 at 18:36
  • This is very useful! One minor point: -1 is a fairly bad default value to return here, just because it represents -0.1 degrees C. Better to use something more implausible like -999. – Chris Rae Sep 17 '15 at 21:44
2

You just change the temp to an int and use that last line you wrote

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);
}
GMBrian
  • 917
  • 1
  • 10
  • 16
  • I can't comment other posts yet :( But on an AVD it will show 0, not null. So it should work on a real device if your TextView displays a 0 The device I'm testing on (one x) returns between 305 and 330 – GMBrian Jul 09 '12 at 18:19