I have pasted my code below.. I somehow dont get the Toast at all even
when the app is triggered on usb cable plugin to the device. It starts
off but force closes.. Pls suggest whats wrong here. I have a intent
filter under receiver for Action_Power_Connected and disconnected
under the manifest file whereas a intentfilter for
action_battery_changed for actual battery details.

package com.utilities.android.batterylog;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

public class BatteryLog extends Activity {
        private TextView mStatus;
    private TextView mLevel;
    private TextView mScale;
    private TextView mHealth;
    private TextView mVoltage;
    private TextView mTemperature;
    private TextView mTechnology;

    private static final int EVENT_TICK = 1;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EVENT_TICK:
                    //updateBatteryStats();
                    sendEmptyMessageDelayed(EVENT_TICK, 1000);
                    break;
            }
        }
    };

    /**
     * Format a number of tenths-units as a decimal string without
using a
     * conversion to float.  E.g. 347 -> "34.7"
     */
    private final String tenthsToFixedString(int x) {
        int tens = x / 10;
        return new String("" + tens + "." + (x - 10*tens));
    }

   /**
    *Listens for intent broadcasts
    */
    private IntentFilter   mIntentFilter;

    private BroadcastReceiver mIntentReceiver = new
BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(action.equals(intent.ACTION_POWER_CONNECTED))
            {
                Toast.makeText(context, "Power Connected", (int)
System.currentTimeMillis());
            }
            else if(action.equals(intent.ACTION_POWER_DISCONNECTED))
            {
                Toast.makeText(context, "Power DisConnected", (int)
System.currentTimeMillis());
            }
            else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                int plugType = intent.getIntExtra("plugged", 0);

                mLevel.setText("level = " +
intent.getIntExtra("level", 0));
                mScale.setText("scale = " +
intent.getIntExtra("scale", 0));
                mVoltage.setText("voltage = " +
intent.getIntExtra("voltage", 0) + " "
                        +
getString(R.string.battery_info_voltage_units));
                mTemperature.setText("temperature = " +
tenthsToFixedString(intent.getIntExtra("temperature", 0))
                        +
getString(R.string.battery_info_temperature_units));
                mTechnology.setText("technology = " +
intent.getStringExtra("technology"));

                int status = intent.getIntExtra("status",
BatteryManager.BATTERY_STATUS_UNKNOWN);
                String statusString;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING)
{
                   statusString =
getString(R.string.battery_info_status_charging);
                    if (plugType > 0) {
                       statusString = statusString + " = " +
getString(
                               (plugType ==
BatteryManager.BATTERY_PLUGGED_AC)
                                       ?
R.string.battery_info_status_charging_ac
                                      :
R.string.battery_info_status_charging_usb);
                   }
               } else if (status ==
BatteryManager.BATTERY_STATUS_DISCHARGING) {
                  statusString =
getString(R.string.battery_info_status_discharging);
               } else if (status ==
BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                  statusString =
getString(R.string.battery_info_status_not_charging);                 }
               else if (status == BatteryManager.BATTERY_STATUS_FULL)
{
                   statusString =
getString(R.string.battery_info_status_full);
               } else {
                  statusString =
getString(R.string.battery_info_status_unknown);
               }
               mStatus.setText(statusString);

               int health = intent.getIntExtra("health",
BatteryManager.BATTERY_HEALTH_UNKNOWN);
               String healthString;
               if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
                   healthString =
getString(R.string.battery_info_health_good);
               } else if (health ==
BatteryManager.BATTERY_HEALTH_OVERHEAT) {
                   healthString =
getString(R.string.battery_info_health_overheat);
               } else if (health ==
BatteryManager.BATTERY_HEALTH_DEAD) {
                   healthString =
getString(R.string.battery_info_health_dead);
               } else if (health ==
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                   healthString =
getString(R.string.battery_info_health_over_voltage);
               } else if (health ==
BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
                   healthString =
getString(R.string.battery_info_health_unspecified_failure);
               } else {
                   healthString =
getString(R.string.battery_info_health_unknown);
               }
               mHealth.setText(healthString);
           }
       }
   };

   @Override
   public void onCreate(Bundle icicle) {
       super.onCreate(icicle);

       setContentView(R.layout.main);

       // create the IntentFilter that will be used to listen
       // to battery status broadcasts
       mIntentFilter = new IntentFilter();
       mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
   }

   @Override
   public void onResume() {
       super.onResume();

       mStatus = (TextView)findViewById(R.id.status);
       mLevel = (TextView)findViewById(R.id.level);
       mScale = (TextView)findViewById(R.id.scale);
       mHealth = (TextView)findViewById(R.id.health);
       mTechnology = (TextView)findViewById(R.id.technology);
       mVoltage = (TextView)findViewById(R.id.voltage);
       mTemperature = (TextView)findViewById(R.id.temperature);

       mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000);

       registerReceiver(mIntentReceiver, mIntentFilter);
   }

   @Override
   public void onPause() {
       super.onPause();
       mHandler.removeMessages(EVENT_TICK);

       // we are no longer on the screen stop the observers
       unregisterReceiver(mIntentReceiver);
   }

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to