http://blog.csdn.net/mjook007/article/details/7990747
在移动开发中,我们经常需要实现一些定时提醒的功能,提醒功能有些定时的,像闹钟一样,有些是根据业务逻辑触发的,这里我主要说一下怎样实现多个定时提醒的实现方法。大言不惭的说下,我在做项目时遇到这个问题再中文网站没有找到任何有意义的相关文章,所以我才会在这里给广大网友分享我的经验!
多次定时重复提醒主要用到的类是AlarmManager(官方API说明),简单说下AlarmManager就是,它能够提供应用系统闹钟服务的接口,允许你的应用在未来某一时间点做某件事。这个类属于系统服务,所以其初始化如下
官方API中,该类的方法并不是很多
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Intent .
|
||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
PendingIntent的初始化是整个功能的最重点,官方API提供的初始化方法有这些:
|
getActivity(Context, , but allows an array of Intents to be supplied.
|
|
getActivity(Context, , but allows an array of Intents to be supplied.
|
|
Context.startActivity(Intent) .
|
|
Context.startActivity(Intent) .
|
|
Context.sendBroadcast() .
|
|
Context.startService() .
|
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
Since: API Level 1Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast()
.
Parameters
contxt | The Context in which this PendingIntent should perform the broadcast. |
---|---|
reque stCode | Private request code for the sender (currently not used). |
intent | The Intent to be broadcast. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT , or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens. |
Returns
- Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
1.intent:intent就是你想要广播的内容,将数据用bundle进行封装,用intent.putExtras(Bundle bundle)方法进行传递。需要注意的是,如果是相同的intent的话,将这个intent作 为参数传进PendingIntent的时候,创建的PendingIntent是已经注册过的pendingIntent,也就是说是已经用这个PendingIntent注册过的广播不会有任何变化,比如说修 改这个广播的触发时间。那么,我们怎么判断俩个Intent是否相同呢,谷歌为我们提供了以下方法
public boolean filterEquals (Intent other)
Since: API Level 1Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does notcompare any extra data included in the intents.
Parameters
other | The other Intent to compare against. |
---|
Returns
- Returns true if action, data, type, class, and categories are the same.
那么,是不是改变了上面说的5个属性中的一个的话就能得到不同的pendingIntent呢?很遗憾,我试验过5个属性,都不能使PendingIntent的equals (Object otherObj)方法返回false(这个方法下面会讲),当然,这也有可能是我的方法有误,广大网友可以指正!
2.flags:看官方API的解释,点这里
这里主要说一下FLAG_UPDATE_CURRENT这个参数,上面我们说过改变intent的参数不会改变对应的pendingIntent注册的广播,如果我们只改 变了intent的额外数据而又想改变广播的内容的话,我们可以把用这个标签。上面的代码改写成这样
这样就可以改变用该pendingIntent注册的广播的内容了那么怎么判断俩个PendingIntent是否一样,谷歌也提供了专门的方法
public boolean equals (Object otherObj)
Since: API Level 1Comparison operator on two PendingIntent objects, such that true is returned then they both represent the same operation from the same package. This allows you to usegetActivity(Context,
,
int, Intent, int)getBroadcast(Context, int, Intent, int)
, or getService(Context, int, Intent, int)
multiple times (even across a process being killed), resulting in different PendingIntent objects but whose equals() method identifies them as being the same operation.
Parameters
otherObj | the object to compare this instance with. |
---|
Returns
-
true
if the specified object is equal to thisObject
;false
otherwise.
上Demo的主要代码:
根据自己项目的需要,改变参数列表和uniqueRequestCode的值,要取消之前已经注册的广播,所传入的uniqueRequestCode必须是一样的!!要设置多个广播,只需改变uniqueRequestCode的值再次执行该方法即可
不知道为什么我上传的源码显示不出来,只好放一个115网盘的下载链接了http://115.com/file/beg2mbgw