设定alarm的话,大概需要用到如下几步:

1.创建一个Intent

2.创建一个PendingIntent

3.得到ALARM_SERVICE的AlarmManager

4.使用AlarmManager的set api,假如是取消,那就使用cancel

android自带的AP对于Alarm的管理是很巧妙的,例如Calendar,它基本上只会给系统设定一个alarm,等这个alarm结束之后,再设定下一个,这种模式很安全,也很有效,我认为这是一个值得学习的模式。

我之前一直有个疑问,设定的alarm,取消时,究竟要传怎样的PendingIntent系统才能知道我要取消的是哪一个

后来经过实验,我发现大概有这样几个参数会起决定作用,在我上面讲的四个步骤的第一步,Intent 的setData,setClass均能区别出,但是

putExtra却是没有用的。另外在PendingIntent.getBroadcast的第二个参数requestCode,查api,Google说这个参数没用的,但是其实它也是可以区别出不同的alarm。

上代码:

[java]  view plain copy
  1. package com.ianc.lily;
  2. import android.app.Activity;
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. public class LaunchActivity extends Activity implements OnClickListener {
  14. static final String LILY_TEST_INTENT = "com.ianc.lilytestintent";
  15. static final String ID = "id";
  16. static final String TIME = "alarm_time";
  17. int id;
  18. Button addBtn;
  19. Button cancelBtn;
  20. /** Called when the activity is first created. */
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. id = 1;
  26. addBtn = (Button) findViewById(R.id.addbtn);
  27. cancelBtn = (Button) findViewById(R.id.cancelbtn);
  28. addBtn.setOnClickListener(this);
  29. cancelBtn.setOnClickListener(this);
  30. }
  31. @Override
  32. public void onClick(View v) {
  33. if (v == addBtn){
  34. AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  35. Intent intent = new Intent(LILY_TEST_INTENT);
  36. intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
  37. intent.setClass(this, LilyReceiver.class);
  38. intent.putExtra(ID, id);
  39. long atTimeInMillis = System.currentTimeMillis() + 5000;
  40. intent.putExtra(TIME, atTimeInMillis);
  41. //          intent.putExtra(LABEL, label);
  42. //          intent.putExtra(TIME, atTimeInMillis);
  43. PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  44. am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
  45. Log.i("lily","add alarm");
  46. }
  47. else if (v == cancelBtn){
  48. AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  49. Intent intent = new Intent(LILY_TEST_INTENT);
  50. intent.setClass(this, LilyReceiver.class);
  51. intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
  52. //          id = 2;
  53. //          Log.i("lily","id = 2");
  54. //          intent.putExtra(ID, id);
  55. PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE);
  56. if (sender != null){
  57. Log.i("lily","cancel alarm");
  58. am.cancel(sender);
  59. }else{
  60. Log.i("lily","sender == null");
  61. }
  62. }
  63. }
  64. }

1.Intent中的setData使用的值,必须和cancel中intent里面setData用的值一样,否则点击cancel是没法取消掉的,或者你不setData,那也是没法取消的

2.Intent中的setClass使用的值也必须和cancel众intent使用的一样,不然也cancel不掉,不setClass也cancel不了

3.Intent中的putExtra是无效的,根本不起区别的作用

4.getBroadcast的第二个参数,一般的ap都是写0,其实假如你前面的intent只有setAction过,那么单纯用reqeustCode也是可以区别不同的alarm的。

5.通常cancel之前可以先用PendingIntent.FLAG_NO_CREATE来判断之前是不是设定了这个alarm,假如没设定,那就不要去调用cancel

补充工程中的次要代码:

AndroidManifest.xml

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.ianc.lily"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".LaunchActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <receiver android:name="LilyReceiver">
  15. <intent-filter>
  16. <action android:name="com.ianc.lilytestintent" />
  17. <data android:mimeType="vnd.android.cursor.item/calendar-alert" />
  18. </intent-filter>
  19. </receiver>
  20. </application>
  21. </manifest>

LilyReceiver.java

[java]  view plain copy
  1. package com.ianc.lily;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. import android.widget.Toast;
  7. public class LilyReceiver extends BroadcastReceiver {
  8. @Override
  9. public void onReceive(Context context, Intent intent) {
  10. String action = intent.getAction();
  11. int id = intent.getIntExtra("id", -1);
  12. long alarmtime= intent.getLongExtra("alarm_time", -1);
  13. Log.i("lily","received action = "+action+", id = "+id+ ", alarmtime = "+alarmtime);
  14. Toast.makeText(context, "received action = "+action+", id = "+id, Toast.LENGTH_SHORT).show();
  15. }
  16. }

android AlarmManager详解,Alarm的设定和取消。相关推荐

  1. android常用api大全,Android API详解大全.pdf

    Android API详解大全 Android -- TextView 一.TextView的API 1.1 结构 java.lang.Object ↳ android.view.View ↳ and ...

  2. Android Drawable 详解

    Android Drawable 详解 @(Technical)[Android, Drawable, StateListDrawable, LayerDrawable, AnimationDrawa ...

  3. 【转】Android菜单详解——理解android中的Menu--不错

    原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...

  4. Android菜单详解——理解android中的Menu

    前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...

  5. Android LayoutInflater详解

    Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...

  6. android Fragments详解

    android Fragments详解一:概述 android Fragments详解二:创建Fragment 转载于:https://my.oschina.net/liangzhenghui/blo ...

  7. android WebView详解,常见漏洞详解和安全源码(下)

    上篇博客主要分析了 WebView 的详细使用,这篇来分析 WebView 的常见漏洞和使用的坑.  上篇:android WebView详解,常见漏洞详解和安全源码(上)  转载请注明出处:http ...

  8. android WebView详解,常见漏洞详解和安全源码(上)

    这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析.  由于博客内容长度,这次将分为上下两篇,上篇详解 WebView ...

  9. android子视图无菜单,Android 菜单详解

    Android中菜单分为三种,选项菜单(OptionMenu),上下文菜单(ContextMenu),子菜单(SubMenu) 选项菜单 可以通过两种办法增加选项菜单,一是在menu.xml中添加,该 ...

最新文章

  1. ubuntu mysql emma_Ubuntu 11.10 MySQL客户端 Emma 6.0 中文乱码解决办法
  2. iOS下JS与OC互相调用(八)--Cordova详解+实战
  3. some learning
  4. 制作openstack Centos镜像 -- Example: CentOS image
  5. c语言24游戏程序,C语言解24点游戏程序
  6. 一名IT经理是如何把项目带崩的。。。
  7. Gym - 215177C 玩游戏
  8. YouTube将关闭原创节目部门
  9. 【Redis】redis JedisDataException: ERR Client sent AUTH, but no password is set
  10. 动态风云--互联网感言(三)
  11. 18100出多少取整_一级注册消防考试难点解析,沥青厂房需要多少个水流指示器及追问...
  12. 旋转屏幕时数据的保存与恢复
  13. Excel导入CSV文件(解决数值转换文本问题)
  14. 快应用开发工具黑屏解决方案
  15. ATF启动(六):bl32(OP-TEE)-->bl33 ATF ending
  16. Beats:在 Docker 里运行 Filebeat
  17. Python Level 4 程序题:输入两个整数,倒序输出
  18. CVPR 2022 结果出炉,最全论文下载及分类汇总(更新中)
  19. wn_concat()函数学习
  20. 【ppt制作软件】Focusky教程 | 怎样实现表格的行列转换?

热门文章

  1. javax.crypto.Cipher类--加密和解密
  2. 【个人整理】一文看尽目标检测算法SSD的核心架构与设计思想
  3. 2.1 SSD算法理论
  4. Thinkphp中的assign() 和 display()
  5. 如何部署JSP应用到阿里云服务器上(一)
  6. C语言 system函数
  7. 优盘婚礼MP4视频播放内容不全的文件修复技术
  8. linux字符终端浏览器-----Lynx
  9. Pycharm中光标变粗 光标进入改写状态
  10. C++ accumulate