首先在设置提醒之前你需要一个入口,比如说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,我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这 ...
随机推荐
- error: 'release' is unavailable: not available in automatic reference counting,该怎么解决
编译出现错误: 'release' is unavailable: not available in automatic reference counting mode.. 解决办法: You nee ...
- 【ContestHunter】【弱省胡策】【Round4】
01分数规划(网络流)+状压DP+树形DP 官方题解地址:http://pan.baidu.com/s/1mg5S5z6 A 好神啊= =第一次写01分数规划 其实分数规划是要求$$ Maximize ...
- HDU1874畅通project续 dijkstra&&floyd
畅通project续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Time Limit: 3000/1000 MS (Java/Others) ...
- C语言:通过指针函数输出二维数组中每个学生的成绩
// // main.c // Pointer_function // // Created by ma c on 15/8/2. // Copyright (c) 2015年 bjsxt. ...
- 4 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- Prototype 原型模式 复制 浅拷贝 clone MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- electron项目中使用jquery不生效的解决方法
才开始接触electron的时候,本来想用bootstrap快速搞一个demo,无奈有些功能没法用,之后才意识到是jquery没发用,之后找了一些资料,比较简单的方式就是在引入jquery之前加入一下 ...
- (转)UnityVS(Visual Studio Tools For Unity)的安装与使用
UnityVS(Visual Studio Tools For Unity)的安装与使用 一些废话 Unity 的开发者们,尤其是微软系的Unity开发者们,用Mono是不是烦死了?你是不是跟我一 ...
- Qt5 for Android: incompatible ABI
I recently installed Qt5 and works like a charm for API 17 and armeabi-v7a.But I added second AVD ...
- Silverlight 之 断点调试
silverlight程序经常会遇到无法调试的情况,下面来总结解决方案. 一.问题描述 在Silverlight开发过程中,经常时不时的会碰到Silverlight无法调试的问题.如下几种情况: 1. ...