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进行定时任务处理的更多相关文章

  1. Java应用集群下的定时任务处理方案(mysql)

    Java应用集群下的定时任务处理方案(mysql)   因为自己有csdn和博客园两个博客, 所以两边都会发一下. csdn地址: http://blog.csdn.net/u012881584/ar ...

  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理

    上一篇文章(https://www.cnblogs.com/meowv/p/12956696.html)成功使用了Redis缓存数据,大大提高博客的响应性能. 接下来,将完成一个任务调度中心,关于定时 ...

  3. Java多机部署下的定时任务处理方案(mysql)

    因为自己有csdn和博客园两个博客, 所以两边都会发一下. csdn地址: http://blog.csdn.net/u012881584/article/details/70194237 今天来说一 ...

  4. 使用ZooKeeper协调多台Web Server的定时任务处理(方案1)

    背景说明: 有一套Web服务程序, 为了保证HA, 需要在多台服务器上部署, 该服务程序有一些定时任务要执行, 现在要保证的是, 同一定时任务不会在多台机器上被同时执行. 方案1 --- 任务级的主备 ...

  5. Springboot:定时任务处理(十三)

    构建一个定时任务的service接口及实现(模拟) 接口:com\applesnt\springboot\service\TaskService.java package com.applesnt.s ...

  6. 定时任务处理-Quartz

    Quartz Scheduler,定时任务 Quartz是一个作业调度系统(a job scheduling system),负责在约定的时间到达时执行(或通知)其他软件控制.是一个Java的定时任务 ...

  7. 使用NODEJS+REDIS开发一个消息队列以及定时任务处理

    作者:RobanLee 原创文章,转载请注明: 萝卜李 http://www.robanlee.com 源码在这里: https://github.com/robanlee123/RobCron 时间 ...

  8. 使用ZooKeeper协调多台Web Server的定时任务处理(方案2)

    承接上个博文, 这次是方案2的实现, 本方案的特点:1. 该方案能很好地从几台服务器中选出一个Master机器, 不仅仅可以用于定时任务场景, 还可以用在其他场景下. 2. 该方案能实现Master节 ...

  9. springboot定时任务处理

    定时任务是一种很常见的应用场景,springboot中的定时任务完全用的spring的那一套,用起来比较简单,需要注意的是线程池配置的那一块 使用 @EnableScheduling 注解就可以开启定 ...

随机推荐

  1. bzoj1145

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1145 神题...... 定义f(abcd)为高度排名为abcd的个数,例如闪电的个数为f(13 ...

  2. requireJS define require

    原文地址:http://blog.csdn.net/xiaogou56a/article/details/21340213 define 用来定义模块 require 用来加载模块 1 因为定义一个模 ...

  3. java实现矩阵连乘的动态规划

    package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...

  4. 打开首页老是加载themes.googleusercontent.com_Wopus问答

    打开首页老是加载themes.googleusercontent.com_Wopus问答 打开首页老是加载themes.googleusercontent.com

  5. Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法

    抛错: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document ...

  6. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  7. python字符串(移除空白,长度,索引,分割,切片,拼接,格式化输出)

    常用功能: 移除空白: >>> name = "meng" >>> name 'meng' >>> name.strip() ...

  8. pyqt托盘例子

    # -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...

  9. ServletRequest中getReader()和getInputStream()只能调用一次的解决办法(转)

    原文地址:http://liwx2000.iteye.com/blog/1542431 原文作者:liwx2000 为了提高项目安全性,拦截非法访问,要给项目增加了一个过滤器,拦截所有的请求,校验是否 ...

  10. JavaScript 精髓整理篇之一(对象篇)postby:http://zhutty.cnblogs.com

    废话篇头: 由于工作关系,所以写博文的时间有那么点~~,其实是输入法太懒了,都是输入法的错~~ 这一系列的博客将总结所有关于JavaScript语言的精髓,适合0基础到大师级别人物阅读. <Ja ...