pendingIntent与Intent在Android中的区别有什么?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Android中pendingIntentp
pendingIntent与Intent在Android中的区别有什么?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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);//第一个参数为自定义的通知唯一标识
--结束END--
本文标题: pendingIntent与Intent在Android中的区别有什么
本文链接: https://www.lsjlt.com/news/223754.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0