悬而未决的Intent

在使用通知的时候发现一种不一样Inetent,一直使用没有管他 最近想总结一下,这个就是PendingIntent (悬而未决的意图)官方文档的描述是:

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int)getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int)

the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.   

一种描述的意图和目标活动的执行  创建这个类的实例得到活动(上下文、int、意图,int),得到活动(上下文、int[],int),得到广播(上下文、int、意图,int)和服务(上下文、int、意图,int);返回的对象可以交给其他应用程序,这样他们就可以执行的动作你描述代表你在稍后的时间

/**
 * Retrieve a PendingIntent that will start a new activity, like calling
 * {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
 * Note that the activity will be started outside of the context of an
 * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
 * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
 *
 * <p class="note">For security reasons, the {@link android.content.Intent}
 * you supply here should almost always be an <em>explicit intent</em>,
 * that is specify an explicit component to be delivered to through
 * {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
 *
 * @param context The Context in which this PendingIntent should start
 * the activity. 
 * @param requestCode Private request code for the sender
 * @param intent Intent of the activity to be launched.
 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
 * or any of the flags as supported by
 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
 * of the intent that can be supplied when the actual send happens.
 *
 * @return Returns an existing or new PendingIntent matching the given
 * parameters.  May return null only if {@link #FLAG_NO_CREATE} has been
 * supplied.
 */
public static PendingIntent getActivity(Context context, int requestCode,
        Intent intent, @Flags int flags) {
    return getActivity(context, requestCode, intent, flags, null);
}
第一个参数 上下文 context 第二个参数requestcode请求码 第三个参数 开启一个Intent 第四个参数Flag

Flag有四种状态

FLAG_ONE_SHOT  
Flag indicating that this PendingIntent can be used only once.
只有一次有效 再次发送的话不起作用 (该方法不常用)
FLAG_NO_CREATE
if the described PendingIntent does not already exist, then simply return null instead of creating it.  
如果描述不存在悬而未决的意图,然后简单地返回null而不是创建它。               
 FLAG_CANCEL_CURRENT
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.  
如果以前的pending intent已经存在的话吹取消之前的 重新创建一个               
FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent 
如果等待意图描述已经存在,那么让它但它额外的数据替换成在这个新的意图
 return getActivity(context, requestCode, intent, flags, null);
public static PendingIntent getActivities(Context context, int requestCode,
        @NonNull Intent[] intents, @Flags int flags, @Nullable Bundle options)
返回的方法第五个参数 bundle对象 可以为空 一些选择

pendingIntent 还有其他的几种方法
public static PendingIntent getBroadcast(Context context, int requestCode,
        Intent intent, @Flags int flags)
开启一个广播 此时的第三个参数Intent就是开启广播似的Intent
public static PendingIntent getService(Context context, int requestCode,
        @NonNull Intent intent, @Flags int flags)
此时的Intent是开启一个服务 不能为空
public static PendingIntent getActivities(Context context, int requestCode,
        @NonNull Intent[] intents, @Flags int flags) {
    return getActivities(context, requestCode, intents, flags, null);
}
开启多个activity此时的intent是一个数组 存放多个Intent对象 该方法不常用
在使用通知的时候经常用到该类型的Intent
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, LoginActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentText("New Message")
        .setContentTitle("消息")
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setTicker("welcome").setContentIntent(activity);
manager.notify(1, builder.build());
当通知发送过以后他的intent 不能马上执行 只有当用户点击通知栏的时候才能触发该Intent 此时PendingIntent就会发挥它的魅力了
本例中涉及到通知会在以后给大家分享 大家在以后使用的时候会发现的魅力所在。

程序猿三大美德

Laziness
The quality that makes you go to great effort to reduce overall energy expenditure. 
It makes you write labor-saving programs that other people will find useful,
and document what you wrote so you don't have to answer so many questions about it.
Hence, the first great virtue of a programmer, Also hence, this book.
懒惰:
是这样一种品质,它使得你花大力气去避免消耗过多的精力。它敦促你写出节省体力的程序,
同时别人也能利用它们。为此你会写出完善的文档,以免别人问你太多问题。
Impatience
The anger you feel when the computer is being lazy. 
This makes you write programs that don't just react to your needs, 
but actually anticipate them. Or at least pretend to. Hence, the second great virtue of a programmer.
急躁:
是这样一种愤怒——当你发现计算机懒洋洋地不给出结果。于是你写出更优秀的代码,能尽快真正的解决问题。至少看上去是这样。
Hubris
Excessive pride, the sort of thing Zeus zaps you for.
Also the quality that makes you write (and maintain) programs that
other people won't want to say bad things about. Hence, the third great virtue of a programmer.
傲慢:
极度的自信,使你有信心写出(或维护)别人挑不出毛病的程序。
 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值