使用AlarmManager进行定时任务处理
1:UploadingService.java
package com.example.service; import com.example.broadcast.AlarmReceiver;
import com.example.utils.DateUtil; import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock; public class UploadingService extends Service{ @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
//to-do
System.out.println("===========:"+DateUtil.getDateByFormat("yyyy-MM-dd HH:mm:ss")); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
int seconds = 1000*3;
long triggerAtTime = SystemClock.elapsedRealtime()+seconds;
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); return super.onStartCommand(intent, flags, startId);
} public static void openService(Context context){
Intent intent = new Intent(context, UploadingService.class);
((Activity)context).startService(intent);
}
}
2:AlarmReceiver.java
package com.example.broadcast; import com.example.service.UploadingService; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, UploadingService.class);
context.startService(i);
}
}
3:MainActivity.java
UploadingService.openService(MainActivity.this);
4:AndroidManifest.xml
<service
android:name="com.example.service.UploadingService"/>
<receiver
android:name="com.example.broadcast.AlarmReceiver"/>
使用AlarmManager进行定时任务处理的更多相关文章
- Java应用集群下的定时任务处理方案(mysql)
Java应用集群下的定时任务处理方案(mysql) 因为自己有csdn和博客园两个博客, 所以两边都会发一下. csdn地址: http://blog.csdn.net/u012881584/ar ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理
上一篇文章(https://www.cnblogs.com/meowv/p/12956696.html)成功使用了Redis缓存数据,大大提高博客的响应性能. 接下来,将完成一个任务调度中心,关于定时 ...
- Java多机部署下的定时任务处理方案(mysql)
因为自己有csdn和博客园两个博客, 所以两边都会发一下. csdn地址: http://blog.csdn.net/u012881584/article/details/70194237 今天来说一 ...
- 使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
背景说明: 有一套Web服务程序, 为了保证HA, 需要在多台服务器上部署, 该服务程序有一些定时任务要执行, 现在要保证的是, 同一定时任务不会在多台机器上被同时执行. 方案1 --- 任务级的主备 ...
- Springboot:定时任务处理(十三)
构建一个定时任务的service接口及实现(模拟) 接口:com\applesnt\springboot\service\TaskService.java package com.applesnt.s ...
- 定时任务处理-Quartz
Quartz Scheduler,定时任务 Quartz是一个作业调度系统(a job scheduling system),负责在约定的时间到达时执行(或通知)其他软件控制.是一个Java的定时任务 ...
- 使用NODEJS+REDIS开发一个消息队列以及定时任务处理
作者:RobanLee 原创文章,转载请注明: 萝卜李 http://www.robanlee.com 源码在这里: https://github.com/robanlee123/RobCron 时间 ...
- 使用ZooKeeper协调多台Web Server的定时任务处理(方案2)
承接上个博文, 这次是方案2的实现, 本方案的特点:1. 该方案能很好地从几台服务器中选出一个Master机器, 不仅仅可以用于定时任务场景, 还可以用在其他场景下. 2. 该方案能实现Master节 ...
- springboot定时任务处理
定时任务是一种很常见的应用场景,springboot中的定时任务完全用的spring的那一套,用起来比较简单,需要注意的是线程池配置的那一块 使用 @EnableScheduling 注解就可以开启定 ...
随机推荐
- Flume配置
http://my.oschina.net/leejun2005/blog/288136#OSC_h1_1 http://blog.cloudera.com/blog/2012/09/analyzin ...
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- PowerShell中调用外部程序和进程操作命令例子
学习PowerShell,我们不指望通过C#编程去搞定所有事情,我们应该记住cmd.exe或者说批处理给我们留下的宝贵财富——通过调用外部程序去解决问题.调用了外部程序,势必就要对进程进行管理,这就是 ...
- HP的笔记本经常蓝屏崩溃 -------athr.sys
因为windows 7才新装不久,没有时间下载配置什么符号表,直接临时下载了WinDbg分析下Dump文件, Probably caused by : athr.sys ( athr+428a5 ) ...
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...
- 线性时间构造普吕弗(Prüfer)序列
tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...
- java反编译工具(XJad)
java反编译工具(XJad) 2.2 绿色版 http://www.cr173.com/soft/35032.html Demo.class ---> Demo.java
- jQuery 遍历 json 方法大全
1.for循环: var obj = { "status":1, "bkmsg":"\u6210\u529f", "bkdata& ...
- Linux备份
Eking<longpeisky@vip.qq.com> 19:35:17 增量备份是针对于上一次备份(无论是哪种备份):备份上一次备份后,所有发生变化的文件. (增量备份过程中,只备份 ...
- hdu 4640 Island and study-sister
bfs+状态压缩求出所有的状态,然后由于第一个节点需要特殊处理,可以右移一位剔除掉,也可以特判.然后采用集合的操作, #pragma comment(linker,"/STACK:10240 ...