如何在android系统在后台运行的任务。? [英] How to run a task in background in android.?

查看:155
本文介绍了如何在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();
            }
        }
    };
}

我要运行该code。使用Android客户端和服务器PC之间socket通信的background.and正在运行。当过我发送请求电池从服务器到客户端,我必须显示在client.please任何人引导我正确path..what电池信息是额外的codeI必须添加在回地面运行它..或者是需要在这一code任何修正..?

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..?

推荐答案

看看的的 Android文档 - 很多你有什么可以重复使用。要创建实施服务 A类业务,要使用它,你使用 bindService 的onCreate()您的活动。

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.

这篇关于如何在android系统在后台运行的任务。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆