关于 PendingIntent 和 Notification 以及 sendTextMessage
发布日期:2021-06-30 11:18:18 浏览次数:2 分类:技术文章

本文共 1748 字,大约阅读时间需要 5 分钟。

 Android的状态栏通知(Notification)

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。

步骤:

1 获取通知管理器NotificationManager,它也是一个系统服务

2 建立通知Notification notification = new Notification(icon, null, when);

3 为新通知设置参数(比如声音,震动,灯光闪烁)

4 把新通知添加到通知管理器

发送消息的代码如下:

//获取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知发生的时间为系统当前时间

//新建一个通知,指定其图标和标题

Notification notification = new Notification(icon, null, when);//第一个参数为图标,第二个参数为短暂提示标题,第三个为通知时间

notification.defaults = Notification.DEFAULT_SOUND;//发出默认声音

notification.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知后自动清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图

notification.setLatestEventInfo(this, “标题”, “我是内容", contentIntent);

mNotificationManager.notify(0, notification);//第一个参数为自定义的通知唯一标识

 

重点是setLatestEventInfo( )方法的最后一个参数!!!!它是一个PendingIntent!!!!!!!!!

这里使用到了PendingIntent(pend本意是待定,不确定的意思)

PendingIntent可以看作是对Intent的包装。PendingIntent主要持有的信息是它所包装的Intent和当前ApplicationContext。正由于PendingIntent中保存有当前ApplicationContext,使它赋予带他程序一种执行的Intent的能力,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent

 

PendingIntent的一个很好的例子:

SmsManager的用于发送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一个参数:destinationAddress 对方手机号码

第二个参数:scAddress 短信中心号码 一般设置为空

第三个参数:text 短信内容

第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个itent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论

第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果

就是说是在"短信发送成功""对方收到此短信"才会激活 sentIntentdeliveryIntent这两个Intent。这也相当于是延迟执行了Intent

 

转载地址:https://it9527.blog.csdn.net/article/details/6178246 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:后台(38)——SqlMapConfig.xml
下一篇:关于ListView与CheckBox的结合(第二版.精华版)

发表评论

最新留言

很好
[***.229.124.182]2024年05月03日 11时42分30秒