首先在设置提醒之前你需要一个入口,比如说onclick事件中,在此不做赘述。
android中使用闹钟进行提醒其实非常简单,你只需要告知系统你想在什么时候被提醒,然后需要一个闹钟的广播接收器,当到你设置的时间时,系统会给你发送一条广播,当你接收到广播后你就可以做一些操作,比如启动你的app,或者跳转到你app中的任何一个界面。废话不多少,直接上代码。
02 |
Intent intent = new Intent(mContext, AlarmReceiver.class); |
03 |
intent.setAction("something"); |
04 |
intent.setType("something"); |
05 |
intent.setData(Uri.EMPTY); |
06 |
intent.addCategory(“something”); |
07 |
intent.setClass(context, AlarmReceiver.class); |
08 |
// 以上给intent设置的四个属性是用来区分你发给系统的闹钟请求的,当你想取消掉之前发的闹钟请求,这四个属性,必须严格相等,所以你需要一些比较独特的属性,比如服务器返回给你的json中某些特定字段。 |
09 |
//当然intent中也可以放一些你要传递的消息。 |
10 |
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmCount, intent, 0); |
11 |
//alarmCount是你需要记录的闹钟数量,必须保证你所发的alarmCount不能相同,最后一个参数填0就可以。 |
12 |
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
13 |
am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); |
14 |
//这样闹钟的请求就发送出去了。time是你要被提醒的时间,单位毫秒,注意不是时间差。第一个参数提醒的需求用我给出的就可以,感兴趣的朋友,可以去google一下,这方面的资料非常多,一共有种,看一下就知道区别了。 |
16 |
Intent intent = new Intent(mContext, AlarmReceiver.class); |
17 |
intent.setAction("something"); |
18 |
intent.setType(something); |
19 |
intent.setData(Uri.EMPTY); |
20 |
intent.addCategory(something); |
21 |
intent.setClass(context, AlarmReceiver.class); |
22 |
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alarmCount, intent, 0); |
23 |
//alarmCount对应到你设定时的alarmCount, |
24 |
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
25 |
am.cancel(pendingIntent); |
27 |
public class AlarmReceiver extends BroadcastReceiver{ |
29 |
private NotificationManager manager; |
32 |
public void onReceive(Context context, Intent intent) { |
33 |
manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); |
35 |
String id = intent.getStringExtra("id"); |
36 |
//MainActivity是你点击通知时想要跳转的Activity |
37 |
Intent playIntent = new Intent(context, MainActivity.class); |
38 |
playIntent.putExtra("id", id); |
39 |
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
40 |
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); |
41 |
builder.setContentTitle("title").setContentText("提醒内容").setSmallIcon(R.drawable.app_icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true).setSubText("二级text"); |
42 |
manager.notify(1, builder.build()); |
到这里闹钟提醒的功能就基本完成了。有问题可以留言。
- Android闹钟设置的解决方案
Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 API19开始AlarmMan ...
- 巧用Windows 7计划任务设置定时提醒
Windows 7系统有个“计划任务”功能,一般人都很少使用.其实,“计划任务”是系统自带的一个很实用的功能,比如说,这个功能可以设置定时提醒,这样在使用电脑时就不会因为太过投入而导致错过重要的事务. ...
- android 闹钟设置问题
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmMana ...
- Android 闹钟设置
在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager 提供了访问系统警报的服务,只要在程序 ...
- Android实例-设置消息提醒(XE8+小米2)
相关资料: 1.官网实例:http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Notification ...
- Android 每天定时提醒功能实现
android要实现定时的功能那肯定就要用到闹铃相关的技术, 那么android闹铃实现是基于 AlarmManager 这个类的,首先我们来看一下它的几个主要的方法. 打开AlarmManager的 ...
- android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动 1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...
- 关于Android中设置闹钟的相对比较完善的解决方案
我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...
- 关于Android中设置闹钟的相对完善的解决方案
前些时候,有人在我「非著名程序员」微信公众号的后台问我有没有设置闹钟的demo,我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这 ...
随机推荐
- 【POJ】【2068】Art Gallery
计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...
- spring中ApplicationContextAware接口使用理解
一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...
- HDU 3910 (13.10.31)
Description Maybe you know “San Guo Sha”, but I guess you didn’t hear the game: “Liang Guo Sha”! Let ...
- Linux文件实时同步,可实现一对多
说明:该功能服务端安装sersync2,客户端安装rsync,原理就是服务端主动推送设定目录下的所有更新的文件到各个客户端rsync接收. rsync大家都知道,是Linux自带的数据同步工具,而se ...
- pytorch 学习问题
https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#sphx-glr-beginner-blitz-n ...
- EMC ViPR all in one page
EMC ViPR 2.0 Product Documentation Index https://community.emc.com/docs/DOC-35557
- 错误: 找不到或无法加载主类 Files\red5-server ,原因与解决办法
因为你把 red5放到了 Program Files 下,而Program Files 中间有个空格,启动路径不允许有空格,换个没空格的路径就OK啦
- ECMAScript5之Object学习笔记(一)
随着IE的逐步追赶,目前到IE11已经能够很好的支持ECMAScript5标准了,其他的现代浏览器像firefox,chrome,opera就更不用说了. 再加上nodejs使得javascript在 ...
- 数据需求统计常用awk命令
原文:http://www.5iops.com/html/2013/script_0418/267.html 1.将时间转换为时间戳 select unix_timestamp('2009-10-26 ...
- (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)
问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...