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 ...
随机推荐
- OC语言-01-面向过程与面向对象思想
一.面向过程 1> 思想 面向过程是一种以过程为中心的最基础编程思想,不支持面向对象的特性. 面向过程是一种模块化程序设计方法 2> 开发方法 面向过程的开发方法是以过程(也可以说是模块) ...
- CocoaPods的安装(图文并茂)OS X 10.11 系统
这里是前言,可以跳过前言部分直接跳到后面"这里正式安装"开始看步骤: 系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 先给一个挺不错的关于 ...
- Gleeo Time Tracker简明使用教程
转载一篇很不错的文章,这款软件还是非常实用的 1 简介 Gleeo Time Tracker是安卓平台下一款相当酷的项目时间记录和管理的软件.说他酷,是因为界面纯黑.而除了这点酷之外,功能也很简单实用 ...
- Git哲学与使用
-- 故国神游,多情应笑我,早生华发. Git是什么? Git是一个版本控制工具,代码管理工具,团队协作工具.它跟SVN等传统工具实现同样的目的:但从某种程度来说,它更快,更灵活.我想绝大多数读者都已 ...
- Eclipse中使用Working Set来管理项目
Eclipse作为一款流行的JavaIDE开发工具,其有很多好用的功能为我们的开发提供帮助.但我们的工作空间中有很多项目时,管理起来就很头疼了. 但是我们又不想更换工作区间,所以我们需要一个更加有效的 ...
- 22 扩展Python - 《Python 核心编程》
- 调用手机话费充值API的SDK编写思路
思路 通过将SDK方法中返回的数据划分为正常返回数据以及错误返回的数据两部分,让调用者更简单的对接口调用错误进行处理. 将SDK调用第三方服务接口的流程划分为: 数据准备,http请求,结果处理三部分 ...
- 2.NopCommerce中文语言包
由于NopCommerce是纯英语环境,给英语不好的管理人员带来诸多不便. NopCommerce支持多语言环境,所以我们只要安装中文语言包,让NopCommerce支持后台中文操作环境. 首先先下载 ...
- Centos配置网卡
大家配置Centos5.5的网卡时,容易忽略的一项就是Linux启动时未启动网卡,其后果很明显,那就是你的Linux机器永远也没有IP地址,下面是一台线上服务器的配置:[root@localhost ...
- SharpDX之Direct2D教程II——加载位图文件和保存位图文件
本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...