PendingIntent的相关基础知识

来源:互联网 发布:手机短信群发软件 编辑:程序博客网 时间:2024/05/17 21:48
最近因为自己不熟悉PendingIntent而犯了很多错。
PendingIntent的设计主要是为了进行一些异步操作,例如在Notification中可以点击Notification然后打开对应的东西,就是通过PendingIntent打开的
创建PendingIntent有三种方式,都是根据Intent中打开的服务而确定使用哪个
一、使用PendingIntent要选择对应的创建方式
Intent中需要打开Activity
public static PendingIntent getActivity(Context context,                                        int requestCode,                                        Intent intent,                                        int flags)
Intent中需要打开,Broadcast
public static PendingIntent getBroadcast(Context context,                                         int requestCode,                                         Intent intent,                                         int flags)
Intent中需要打开Service
public static PendingIntent getService(Context context,                                       int requestCode,                                       Intent intent,                                       int flags)
二、PendingIntent的参数必须清楚了解
context - 不解释
requestCode - 这个int值设置成相同的话,只能打开同一个服务,例如需要打开多个Service,则,这个值不能一样
intent - 设置打开的服务
flags - 这个值主要是和PendingIntent自身相关的,设置成FLAG_UPDATE_CURRENT:则每次都重新创建一个新的,用得比较多,FLAG_CANCEL_CURRENT如果有相同的PendingIntent取消前面PendingIntent,再生成一个新的,FLAG_ONE_SHOT只能使用一次,再次修改将不会成功。
三、由于PendingIntent使用在异步操作上,很多服务在使用了不恰当的PendingIntent时并不会显示异常,而仅仅是不进行相关操作而已,如AlarmManager中即使Intent中打开的是一个未配置的Service,也完全不会提示错误,而只是不打开Service,这个时候应细致的将每一次参数传递,函数调用都清晰显示,不然很难找到问题
PendingIntent笔记,以后再遇到相关问题再做添加和修改。
0 0
原创粉丝点击