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);

Android使用service后台更新计划任务的更多相关文章

  1. 在Android中实现service动态更新UI界面

    之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...

  2. android中service启动后台程序

    Service是Android中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the b ...

  3. 【Android 】Service 全面总结

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  4. Android DropBoxManager Service

    Android DropBoxManager Service 什么是 DropBoxManager ? Enqueues chunks of data (from various sources – ...

  5. Android它Service

    服务是一段代码的后台执行. 无法处理,也不是螺纹,但它是在进程和线程的执行. Android该服务与Activity不同,不能与用户交互,无法启动自己. 媒体播放服务.当用户退出媒体选择用户界面,不过 ...

  6. android 使用 service 实现音乐

    今天的球员趁service.播放音乐service结束,进度条activity结束,因此,基础工作activity和service互动,本文将使用IBinder互动.主要activity能够调用ser ...

  7. android基础---->service的生命周期

    服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...

  8. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  9. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

随机推荐

  1. iOS-绘图(Quartz2D)的简单使用(原创)

    前言 附上绘图demo--https://github.com/yangfangxue/YFX_Quartz-2D 什么是Quartz2D? Quartz 2D是一个二维图形绘制引擎,支持ios环境和 ...

  2. Swift面向对象基础(中)——Swift中的存储属性和计算属性

    学习来自<极客学院> 1.存储属性:存储在类.结构体里的变量或者常量 2.分为:实例存储属性.类型存储属性 3.所有的存储属性必须显示的指定初始值,在定义时或者构造器当中指定 4.可选类型 ...

  3. Sql server存储过程中常见游标循环用法

    用游标,和WHILE可以遍历您的查询中的每一条记录并将要求的字段传给变量进行相应的处理 DECLARE ), ), @A3 INT DECLARE YOUCURNAME CURSOR FOR SELE ...

  4. Java基础の第一弹

    一.虚拟机的工作机制 (1) :通过 ClassLoader 寻找和装载 class 文件 (2) :解释字节码成为指令并执行,提供 class 文件的运行环境 (3) :进行运行期间垃圾回收 (4) ...

  5. Hive Experiment 2(表动态分区和IDE)

    1.使用oracle sql developer 4.0.3作为hive query的IDE. 下载hive-jdbc driver http://www.cloudera.com/content/c ...

  6. [转]jQuery插件实现模拟alert和confirm

    本文转自:http://www.jb51.net/article/54577.htm (function () { $.MsgBox = { Alert: function (title, msg) ...

  7. [麦先生]初学Laravel框架与ThinkPHP的不同(1)

    作为一个PHP菜鸟初学Laravel框架 在学习过程中我发现了其与TP框架的不同点,由于时间问题和认识还不够完善我先写出其中几点,有错误的地方希望各位大牛斧正... 1.渲染模版方式的不同:在Lara ...

  8. selenium如何解决IE自动填充表单问题

    有时候用selenium会碰到自动填充表单的问题,如输入用户名后,密码自动填充,此时再填充密码会导致登录失败,解决办法:每个输入框都调用clear()方法

  9. 设计模式笔记感悟 - Creational篇

    body,td,p { // 这对大括号里描述网页的背景 margin-left:40px; margin-right:40px; font-size: 10pt; } div.vim { width ...

  10. POJ 2263 Heavy Cargo 多种解法

    好题.这题可以有三种解法:1.Dijkstra   2.优先队列   3.并查集 我这里是优先队列的实现,以后有时间再用另两种方法做做..方法就是每次都选当前节点所连的权值最大的边,然后BFS搜索. ...