IntentService+BroadcastReceiver 实现定时任务
效果图:![]()
AlramIntentService
package com.example.admin.water;
import android.app.AlarmManager;import android.app.IntentService;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.SystemClock;
public class AlramIntentService extends IntentService {
public AlramIntentService() { super("AlramIntentService"); }
@Override protected void onHandleIntent( Intent intent) { new Thread(new Runnable() { @Override public void run() { NotificationUtils.remindUserBecauseCharging(AlramIntentService.this);
} }).start(); AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); long triggerAtTime=SystemClock.elapsedRealtime()+60*1000; Intent i=new Intent(this,AlarmReceiver.class); PendingIntent pendingIntent=PendingIntent.getBroadcast(this,0,i,0); alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pendingIntent);
}}
AlarmReceiver
package com.example.admin.water;
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=new Intent(context,AlramIntentService.class); context.startService(intent);
}}
最后在MainActivity加上Intent i=new Intent(this,AlramIntentService.class);startService(i);
github项目源码:https://github.com/NeoWu55/Android-Water
IntentService+BroadcastReceiver 实现定时任务的更多相关文章
- [Xamarin.Android] 結合Windows Azure與Google cloud message 來實現Push Notification (转帖)
這一篇要討論如何使用Xamarin.Android 整合GCM以及Windows Azure來實作Android手機上的推播通知服務. 這篇文章比較著重概念的部分,在開始讀這篇之前,也可以先參考一下X ...
- AlarmManager定时闹钟
一.AlarmManager介绍: AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来 ...
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- Android IntentService完全解析 当Service遇到Handler
一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...
- android: 后台执行的定时任务
Android 中的定时任务一般有两种实现方式,一种是使用 Java API 里提供的 Timer 类, 一种是使用 Android 的 Alarm 机制.这两种方式在多数情况下都能实现类似的效果,但 ...
- Android IntentService 与Alarm开启任务关闭任务
1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...
- Android Service 通过 BroadcastReceiver 更新Activity UI
1:MainActivity.java public class MainActivity extends Activity { private TextView tvInfo = null; pri ...
- AlarmReceiver 与IntentService的使用
1:GetSmsService.java public class GetSmsService extends IntentService{ private AlarmManager alarmMan ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
随机推荐
- C# 如何更改Word语言设置
一般在创建或者打开一个Word文档时,如果没有进行过特殊设置的话,系统默认的输入语言的是英语输入,但是为适应不同的办公环境,我们其实是需要对文字嵌入的语言进行切换的,因此,本文将介绍如何使用免费版组件 ...
- Android项目刮刮奖详解(三)
Android项目刮刮奖详解(二) 前言 上一期我们已经实现了一个简易的刮刮卡功能,这一期我们来将其完善一下 目标 将刮刮奖的宽高改为合适高度 将刮刮奖位置居中 将信息层的图片换成文字(重点) 实现 ...
- Android开发过程中的坑及解决方法收录(六)
1. file.listFiles 空指针异常 最近在弄个小项目,类似一个文件管理器,需要获得手机存储里的目录之后显示,但是运行过程中出现错误,搜索了资料,得出了以下的解决办法 问题产生的原因: an ...
- 开源前端脚本错误监控及跟踪解决项目BadJS试用
摘要: 试用BadJS. 原文:开源前端脚本错误监控及跟踪解决项目-BadJS 试用 作者:过错 Fundebug经授权转载,版权归原作者所有. BadJS 是 一个web 前端脚本错误监控及跟踪项目 ...
- js之正则的坑
首先给一个神奇的图: 我的反应,精分吧!一会儿true一会儿false的... 后来发现,把g去掉后就正常了,那这是为什么呢??lastIndex惹得鬼! 正文: lastIndex 全局正则表达是, ...
- Go开发之路 -- 流程控制
1. if else if 条件 { } else { // else必须写在这里 } // 写一个程序, 从终端读取输入, 并转成整数. 如果转成整数出错, // 则输出'can not conve ...
- iOS----------viewcontroller中的dealloc方法不调用
ios的viewcontroller生命周期是 init -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppe ...
- Visual Studio 无法记忆标签页、断点等的解决办法
1.到工程目录删除*.suo文件 2.*.suo默认是隐藏的,需要打开查看隐藏文件的开关
- android 开发之 ListView 与Adapter 应用实践
在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...
- JHipster技术栈定制 - 基于UAA的微服务之间安全调用
本文通过代码实例演示如何通过UAA实现微服务之间的安全调用. uaa: 身份认证服务,同时也作为被调用的资源服务.服务端口9999. microservice1: 调用uaa的消费者服务,服务端口80 ...