pendingIntent与Intent在Android中的区别有什么-创新互联

pendingIntent与Intent在Android中的区别有什么?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联公司主营松滋网站建设的网络公司,主营网站建设方案,重庆APP软件开发,松滋h5小程序开发搭建,松滋网站营销推广欢迎松滋等地区企业咨询

Android中pendingIntent

pendingIntent字面意义:等待的,未决定的Intent。

要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)  分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。

参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。

pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。

主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。

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);//第一个参数为自定义的通知唯一标识

网页标题:pendingIntent与Intent在Android中的区别有什么-创新互联
分享网址:https://www.cdcxhl.com/article12/phjdc.html

成都网站建设公司_创新互联,为您提供网站营销网站制作小程序开发网站收录网页设计公司搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作