首先在设置提醒之前你需要一个入口,比如说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,我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这 ...
随机推荐
- maven+jenkins+jmeter性能测试:maven把项目依赖拷贝到项目指定位置
先上pom.xml配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- scala编程第19章学习笔记(1)——类型参数化
一.queues函数式队列 函数式队列是一种具有以下三种操作方式的数据结构: head 返回队列的第一个元素. tail 返回除第一个元素之外的队列. scala> import scala.c ...
- iOS:NSBundle的具体介绍
NSBundle介绍:它是一个单例类,用来加载资源 bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-i ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- 黑马day12 DbUtils的介绍
简单介绍: DbUtils为不喜欢hibernate框架的钟爱.它是线程安全的,不存在并发问题. 使用步骤: 1. QueryRunner runner=new QueryRunner(这里写数据源. ...
- idea 设置代码的颜色
- URL参数转换对象
var parseQueryString = function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_para = /([^&=]+ ...
- 免费资源:JellyFish的iOS8应用图标集
本地下载 包含设计和PNG效果图片的iOS8的图标集合.
- ORACLE关于锁表查询的部分SQL
http://www.cnblogs.com/quanweiru/archive/2012/08/28/2660700.html --查询表空间名称和大小 SELECT UPPER (F.TABLES ...
- readonly 和 disable的区别
Readonly和Disabled它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / password)和textar ...