android AlarmManager详解,Alarm的设定和取消。
设定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。
上代码:
- package com.ianc.lily;
- import android.app.Activity;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class LaunchActivity extends Activity implements OnClickListener {
- static final String LILY_TEST_INTENT = "com.ianc.lilytestintent";
- static final String ID = "id";
- static final String TIME = "alarm_time";
- int id;
- Button addBtn;
- Button cancelBtn;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- id = 1;
- addBtn = (Button) findViewById(R.id.addbtn);
- cancelBtn = (Button) findViewById(R.id.cancelbtn);
- addBtn.setOnClickListener(this);
- cancelBtn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (v == addBtn){
- AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(LILY_TEST_INTENT);
- intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
- intent.setClass(this, LilyReceiver.class);
- intent.putExtra(ID, id);
- long atTimeInMillis = System.currentTimeMillis() + 5000;
- intent.putExtra(TIME, atTimeInMillis);
- // intent.putExtra(LABEL, label);
- // intent.putExtra(TIME, atTimeInMillis);
- PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
- am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
- Log.i("lily","add alarm");
- }
- else if (v == cancelBtn){
- AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(LILY_TEST_INTENT);
- intent.setClass(this, LilyReceiver.class);
- intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
- // id = 2;
- // Log.i("lily","id = 2");
- // intent.putExtra(ID, id);
- PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE);
- if (sender != null){
- Log.i("lily","cancel alarm");
- am.cancel(sender);
- }else{
- Log.i("lily","sender == null");
- }
- }
- }
- }
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
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ianc.lily"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".LaunchActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name="LilyReceiver">
- <intent-filter>
- <action android:name="com.ianc.lilytestintent" />
- <data android:mimeType="vnd.android.cursor.item/calendar-alert" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
LilyReceiver.java
- package com.ianc.lily;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import android.widget.Toast;
- public class LilyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- int id = intent.getIntExtra("id", -1);
- long alarmtime= intent.getLongExtra("alarm_time", -1);
- Log.i("lily","received action = "+action+", id = "+id+ ", alarmtime = "+alarmtime);
- Toast.makeText(context, "received action = "+action+", id = "+id, Toast.LENGTH_SHORT).show();
- }
- }
android AlarmManager详解,Alarm的设定和取消。相关推荐
- android常用api大全,Android API详解大全.pdf
Android API详解大全 Android -- TextView 一.TextView的API 1.1 结构 java.lang.Object ↳ android.view.View ↳ and ...
- Android Drawable 详解
Android Drawable 详解 @(Technical)[Android, Drawable, StateListDrawable, LayerDrawable, AnimationDrawa ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- Android菜单详解——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
- Android LayoutInflater详解
Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...
- android Fragments详解
android Fragments详解一:概述 android Fragments详解二:创建Fragment 转载于:https://my.oschina.net/liangzhenghui/blo ...
- android WebView详解,常见漏洞详解和安全源码(下)
上篇博客主要分析了 WebView 的详细使用,这篇来分析 WebView 的常见漏洞和使用的坑. 上篇:android WebView详解,常见漏洞详解和安全源码(上) 转载请注明出处:http ...
- android WebView详解,常见漏洞详解和安全源码(上)
这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析. 由于博客内容长度,这次将分为上下两篇,上篇详解 WebView ...
- android子视图无菜单,Android 菜单详解
Android中菜单分为三种,选项菜单(OptionMenu),上下文菜单(ContextMenu),子菜单(SubMenu) 选项菜单 可以通过两种办法增加选项菜单,一是在menu.xml中添加,该 ...
最新文章
- ubuntu mysql emma_Ubuntu 11.10 MySQL客户端 Emma 6.0 中文乱码解决办法
- iOS下JS与OC互相调用(八)--Cordova详解+实战
- some learning
- 制作openstack Centos镜像 -- Example: CentOS image
- c语言24游戏程序,C语言解24点游戏程序
- 一名IT经理是如何把项目带崩的。。。
- Gym - 215177C 玩游戏
- YouTube将关闭原创节目部门
- 【Redis】redis JedisDataException: ERR Client sent AUTH, but no password is set
- 动态风云--互联网感言(三)
- 18100出多少取整_一级注册消防考试难点解析,沥青厂房需要多少个水流指示器及追问...
- 旋转屏幕时数据的保存与恢复
- Excel导入CSV文件(解决数值转换文本问题)
- 快应用开发工具黑屏解决方案
- ATF启动(六):bl32(OP-TEE)-->bl33 ATF ending
- Beats:在 Docker 里运行 Filebeat
- Python Level 4 程序题:输入两个整数,倒序输出
- CVPR 2022 结果出炉,最全论文下载及分类汇总(更新中)
- wn_concat()函数学习
- 【ppt制作软件】Focusky教程 | 怎样实现表格的行列转换?