如何在android中运行后台任务。?

时间:2010-09-09 07:45:20

标签: 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();
            }
        }
    };
}

我希望运行此代码以在后台运行并使用Android客户端和服务器PC之间的套接字通信。当我从服务器向客户端发送电池请求时,我必须在客户端显示电池信息。请任何人指导我正确的路径..我必须添加的额外代码,以便在后台运行..或任何此代码需要更正..?

2 个答案:

答案 0 :(得分:3)

您可以实现扩展Service的类。服务适用于不需要与用户交互的长时间运行的任务。请注意,服务也在您的应用程序主线程中运行,导致应用程序在执行IO时挂起,因此您可能希望生成thread

答案 1 :(得分:2)

看一下Android documentation - 你可以重复使用的很多东西。要创建服务,您要实施Service类,要使用它,请在活动onCreate()中使用bindService