Q:

How to run a task in background in android.?

public class Battery1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }

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

        IntentFilter filter = new IntentFilter();

        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(mBroadcastReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(mBroadcastReceiver);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                int status = intent.getIntExtra("status", 0);
                int health = intent.getIntExtra("health", 0);
                boolean present = intent.getBooleanExtra("present",false);
                int level = intent.getIntExtra("level", 0);
                int scale = intent.getIntExtra("scale", 0);
                int icon_small = intent.getIntExtra("icon-small", 0);
                int plugged = intent.getIntExtra("plugged", 0);
                int voltage = intent.getIntExtra("voltage", 0);
                int temperature = intent.getIntExtra("temperature",0);
                String technology = intent.getStringExtra("technology");

                String statusString = "";

                switch (status) {
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    statusString = "unknown";
                    break;
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    statusString = "charging";
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    statusString = "discharging";
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    statusString = "not charging";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    statusString = "full";
                    break;
                }

                String healthString = "";

                switch (health) {
                case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                    healthString = "unknown";
                    break;
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    healthString = "good";
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    healthString = "overheat";
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    healthString = "dead";
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    healthString = "voltage";
                    break;
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    healthString = "unspecified failure";
                    break;
                }

                String acString = "";

                switch (plugged) {
                case BatteryManager.BATTERY_PLUGGED_AC:
                    acString = "plugged ac";
                    break;
                case BatteryManager.BATTERY_PLUGGED_USB:
                    acString = "plugged usb";
                    break;
                }

               Toast.makeText(context, 
                          "status     :" + statusString               +
                       ";\nhealth     :" + healthString               +
                       ";\npresent    :" + String.valueOf(present)    +
                       ";\nlevel      :" + String.valueOf(level)      + 
                      "%;\nscale      :" + String.valueOf(scale)      + 
                       ";\nicon_small :" + String.valueOf(icon_small) +
                       ";\nplugged    :" + acString                   + 
                       ";\nvoltage    :" + String.valueOf(voltage)    + 
                       ";\ntemperature:" + String.valueOf(temperature)+
                       ";\ntechnology :" + technology, Toast.LENGTH_LONG).show();
            }
        }
    };
}

i want to run this code to be running in background.and using socket communication between Android client and server PC. when ever i send the request for battery from server to client i have to display the battery information on client.please anyone guide me in correct path..what is the extra code i have to add for running it in back ground..or any correction is needed in this code..?

A:

You can implement a class extending Service. A service is suitable for long running tasks not requiring interaction with the user. Note that a service is also running in you applications main thread, causing the application to hang when performing IO, so you may want to spawn a thread.

A:

Take a look at the Android documentation - a lot of what you have can be re-used. To create a service you implement the Service class, to use it you use bindService in onCreate() in your activity.

