Android使用service后台更新计划任务
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后台更新计划任务的更多相关文章
- 在Android中实现service动态更新UI界面
之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...
- android中service启动后台程序
Service是Android中一个类,它是Android四大组件之一,使用Service可以在后台执行长时间的操作( perform long-running operations in the b ...
- 【Android 】Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- Android DropBoxManager Service
Android DropBoxManager Service 什么是 DropBoxManager ? Enqueues chunks of data (from various sources – ...
- Android它Service
服务是一段代码的后台执行. 无法处理,也不是螺纹,但它是在进程和线程的执行. Android该服务与Activity不同,不能与用户交互,无法启动自己. 媒体播放服务.当用户退出媒体选择用户界面,不过 ...
- android 使用 service 实现音乐
今天的球员趁service.播放音乐service结束,进度条activity结束,因此,基础工作activity和service互动,本文将使用IBinder互动.主要activity能够调用ser ...
- android基础---->service的生命周期
服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
随机推荐
- javascripts 实习自动提交表单 onsubmit
html: <form id="formwb" onsubmit="return setPassword();"> <script> d ...
- openstack 上床镜像, 创建网络, 创建虚拟机 命令
==================================================================================================== ...
- TFS配置过程中的错误
有些人在配置TFS的过程中会报出[以前的更新或安装需要重新启动操作系统.……]的错误,但会发现无论重启多次操作系统,再配置的时候依然会报这个错误,很是让人苦恼哦. 这个错误在安装SharePoint的 ...
- hibernate数据库连接池
访问数据库,需要不断的创建和释放连接,假如访问量大的话,效率比较低级,服务器消耗大: 使用数据库连接池,我们可以根据实际项目的情况,定义连接池的连接个数,从而可以实现从连接池获取连接,用户放回到连接池 ...
- hibernate一对一关系实现
按照主键映射,按照外键映射 Address.hbm.xml: <?xml version="1.0"?><!DOCTYPE hibernate-mapping P ...
- SQL2008 提示评估期已过的解决方法
提示窗口: 解决步骤: 第一步:进入SQL2008配置工具中的安装中心 第二步:再进入维护界面,选择版本升级 第三步:进入产品密钥,输入密钥 第四步:一直点下一步,直到升级完毕. SQL Server ...
- ACM竞赛高手比其他程序员水平高很多吗?
1. ACM是一种很直接的评价程序员水平的体系 2. ACM竞赛会带来很多机遇(深造or工作),同时又是一个不小的挑战 3. 为竞赛而竞赛的事情不可取 详细点击这里
- O(N)时间的排序
题目:某公司有几万名员工,请完成一个时间复杂度为O(n)的算法对该公司员工的年龄作排序,可使用O(1)的辅助空间. 要注意分析题目,一般排序要么是O(n^2),要么是O(nlogn).但这里题目特别强 ...
- HTML常用文本元素
HTML是超文本标记语言,它提供网页的具体内容,包括文本.表单.图像.表格.链接.多媒体.列表等.其中文本是我们遇到的最多的展示内容.正确的使用文本标签,会使页面具有语义化,也有利于SEO. 文本标签 ...
- SQLConnect_ref.cpp
// SQLConnect_ref.cpp // compile with: odbc32.lib #include <windows.h> #include <sqlext.h&g ...