Service是Android的四大组件之一,这里就不再过多的去描述,下面主要实现启动应用时候利用service后台执行计划任务,退出应用后,关闭service,只存在整个应用的周期中。

首先使用service需要在manifest中的Application 中注册

  <service android:name=".WxService" android:enabled="true" android:exported="true" >  </service>       

写一个WxService类,继承于Service,然后实现它的一些方法。

public class WxService extends Service{    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();        Timer timer = new Timer();        timer.schedule(new Work(),0, 30000);            }    @Override    public void onStart(Intent intent, int startId) {        // TODO Auto-generated method stub        super.onStart(intent, startId);    }
}

在onCreate()方法中去调度计划,下面介绍一个这个Schedule类。

public void schedule(TimerTask task,                     long delay,                     long period)Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.Parameters:task - task to be scheduled.delay - delay in milliseconds before task is to be executed.period - time in milliseconds between successive task executions.Throws:IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.说明:该方法会在指定的延时后执行任务,并且在设定的周期定时执行任务。

计划任务

 class Work extends TimerTask{        @Override        public void run() {            // TODO Auto-generated method stub        Message message = new Message();        message.what=1;        handler.sendMessage(message);        }      }          Handler handler = new Handler()     {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            if(msg.what==1)            {                new Updata().execute();            }        }              };


在Activity中启动Service

Intent i  = new Intent(context,WxService.class);
context.startService(i);

退出应用程序时候,停止服务

Intent i  = new Intent(context,WxService.class);context.stopService(i);

更多相关文章

  1. Android异步任务AsyncTask
  2. Android中startService的使用及Service生命周期
  3. Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
  4. Android——横竖屏切换以及数据保存
  5. Android(安卓)任务、进程和线程
  6. activity生命周期及横竖屏切换
  7. 【Android(安卓)Linux内存及性能优化】(八) 系统性能分析工具
  8. Android中的Activity生命周期
  9. 解开Android应用程序组件Activity的"singleTask"之谜(2)

随机推荐

  1. 《Android(安卓)Activity》活动的介绍和
  2. Android(安卓)setContentView方法解析(一)
  3. Fragment 视图重绘问题
  4. window7 android-ndk-r10d 编译ffmpeg 3.
  5. android 学习四 加载图片
  6. Android用户界面 UI组件--TextView及其子
  7. Paint---PathEffect(路径效果)
  8. [Android(安卓)SQLite]数据存储与访问 -
  9. Android(安卓)应用第一次运行时,引导页面
  10. (详解)Eclipse3.6搭建 Android(安卓)2.2