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)和广播接受 ...
随机推荐
- [Go] golang原子函数实现goroutine同步
启动了两个goroutine,并完成一些工作.在各自循环的每次迭代之后,在goroutine 会使用LoadInt64 来检查shutdown 变量的值.这个函数会安全地返回shutdown 变量的一 ...
- Dynamics 365 POA表记录的查询
微软动态CRM专家罗勇 ,回复313或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . PrincipalO ...
- NET Core应用中使用缓存
NET Core应用中使用缓存 .NET Core针对缓存提供了很好的支持 ,我们不仅可以选择将数据缓存在应用进程自身的内存中,还可以采用分布式的形式将缓存数据存储在一个“中心数据库”中.对于分布式缓 ...
- Dynamics 365 Online-Security Updates On TLS 1.2
不仅仅是Dynamics 365,现在MS许多产品都开始主推使用TLS1.2,所以在日常开发中,需要注意这部分的改动. 如果访问某个服务,出现错误信息类似于“Could not create SSL/ ...
- ArcGIS for JavaScript学习(二)Server发布服务
一 ArcGIS for Server 安装.配置 (1)双击setup (2)点击下一步完成安装 (3)配置 a 登录Manager 开始—>程序—>ArcGIS—>Manager ...
- Thinkphp5整合微信扫码支付开发实例
ThinkPHP框架是比较多人用的,曾经做过的一个Thinkphp5整合微信扫码支付开发实例,分享出来大家一起学习 打开首页生成订单,并显示支付二维码 public function index() ...
- Java新知识系列 八
什么是死锁,死锁的原因和必要条件: []什么是死锁,死锁的原因和必要条件: 死锁:死锁的原因在于进程在等待其它进程占有的某些资源,而自身的资源又被其它进程等待着,造成了死循环. 出现死锁的 ...
- java新知识系列 六
sleep和wait的区别有: Servlet方法的使用 方法重写的规则,以及两同两小一大原则: DispatcherServlet的解析 依赖注入DU和控制反转Ioc AOP和OOP的区别 Spri ...
- 基于Git项目管理客户端SourceTree的免注册安装及远程连接方法
作为程序员,不可避免的要在github上查询代码,而在企业项目中,为了使得项目好管理需要使用项目管理客户端,所以接下来详细讲解一下基于git的sourceTree在windows系统下的安装及与Git ...
- MongoDB更需要好的模式设计 及 案例赏析
一 挑战 设计从来就是个挑战. 当我们第一次接触数据库,学习数据库基础理论时,都需要学习范式,老师也一再强调范式是设计的基础.范式是这门课程中的重要部分,在期末考试中也一定是个重要考点.如果我们当年 ...