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. C#反射 获取程序集信息和通过类名创建类实例(转载)

    C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...

  2. 脱机BT transmission

    脱机BT transmission http://192.168.1.1:9091 也可以在使用Transmission Remote软件进行管理,如果下载太慢检查QoS 如果开了防火墙,需要打开默认 ...

  3. wireshark保存部分报文的方法

    抓包时采用下列两种命令: tcpdump –s 0 –i eth0 host IP1 and IP2 and port 5060 and 5080 –v –w file1.pcap 与 tcpdump ...

  4. SQL语言的组成

    在正式学习SQL语言之前,首先让我们对SQL语言有一个基本认识,介绍一下SQL语言的 组成: 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成, ...

  5. zoj3802:easy 2048 again(状压dp)

    zoj月赛的题目,非常不错的一个状压dp.. 题目大意是一个一维的2048游戏 只要有相邻的相同就会合并,合并之后会有奖励分数,总共n个,每个都可以取或者不取 问最终得到的最大值 数据范围n<= ...

  6. 支持多文件上传,预览,拖拽,基于bootstra的上传插件fileinput 的ajax异步上传

    首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...

  7. windows 查看端口被占用

    C:\Users\xxxx> 根据端口找到进程14716 C:\Users\xxxx>tasklist|findstr "14716"node.exe 14716 Co ...

  8. MVC MVC 路由详解

    在项目中我们引用了System.Web.Routing;   Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...

  9. Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)

    今天下午在Mybatis项目中.实现分页.由于我是后加入项目中的,Leader用的是PageHelper这个组件.可是我在实际使用的过程中遇到了2个大问题. 1.p=2#comments" ...

  10. centos6.5配置无线网络

    由于安装的是服务器版,所以开机无法连接网络,以下这些情况都是针对驱动已经安装OK.按步骤操作如下(以下操作默认都是在超级管理员权限下进行): 1.测试电脑是否安装wpa_supplicant,测试方法 ...