PendingIntent的flag
PendingIntent 有4个flag值
flag值是获取PendingIntent对象时getActivity()、getBroadcast()、getSewrvice()时传入的参数,flag值的作用是管理一个PendingIntent,控制其是否有效,通常与requestCode结合使用,requestCode标记了一个PeningIntent
FLAG_CANCEL_CURRENT:当定义了一个PendingIntent后,再次定义一个PendingIntent对相同的requestCode而言则会将第一个PendingIntent cancle掉(改变的是PendingIntent中的intent的extra),重新创建一个新的PendingIntent,原先的PendingIntent失去作用,这样则只有最新的PendingIntent有效
在消息栏通知中:
requestCode相同时:
消息栏通知中的manager.notify()的第一个参数相同时
实例化PendingIntent时的参数Intent的putExra(this,xxx.class);xxx不变//对同一条消息进行修改,对PendingIntent而言只是修改Intent的Extra
消息栏通知中的manager.notify()的第一个参数不同时
会生成一条新的消息,原消息的内容不变,点击效果没有了,点击会以新消息为标准//会将第一条消息的PendingIntent Cancle掉,生成一个新的PendingIntent,只有第二条有点击反应
requestCode不同时:
消息栏通知中的manager.notify()的第一个参数相同时
消息会更新,实例化PendingIntent时的参数Intent的putExra(this,xxx.class);xxx会更新,以第二次的PendingIntent为准
消息栏通知中的manager.notify()的第一个参数不同时
会生成一条新的消息,内容也是新的,原消息的内容不变,点击效果不变//生成两个新的PendingIntent,都有效果
FLAG_NO_CREATE :PendingIntent没有作用,对消息而言没有点击效果
FLAG_ONE_SHOT:PendingIntent只能被使用一次,多次创建PendingIntent也只能使用一次
FLAG_UPDATE_CURRENT:如果对requestCode相同的则会都更新为最新的PendingIntent的Intent的Extra
requestCode相同时:
消息栏通知中的manager.notify()的第一个参数相同时
内容更新,实例化PendingIntent时的参数Intent的putExra(this,xxx.class);xxx不变
消息栏通知中的manager.notify()的第一个参数不同时
会生成一条新的消息,原消息的内容不变,点击效果变为第二个PendingIntent,消息的也有点击效果//都更新为最新的PendingIntent
requestCode不同时:
消息栏通知中的manager.notify()的第一个参数相同时
内容更新,实例化PendingIntent时的参数Intent的putExra(this,xxx.class);xxx更新为最新的
消息栏通知中的manager.notify()的第一个参数不同时
会生成一条新的消息,原消息的内容不变,点击效果也不变,新消息也有点击效果//都更新为最新的
public class MainActivity extends Activity {
private TextView num;
private TextView content;
private CheckBox box;
private CheckBox box1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = (TextView) findViewById(R.id.num);
content = (TextView) findViewById(R.id.content);
box = (CheckBox) findViewById(R.id.checkBox1);
box1 = (CheckBox) findViewById(R.id.checkBox);
Button FLAG_CANCEL_CURRENT = (Button) findViewById(R.id.FLAG_CANCEL_CURRENT);
Button FLAG_UPDATE_CURRENT = (Button) findViewById(R.id.FLAG_UPDATE_CURRENT);
Button FLAG_ONE_SHOT = (Button) findViewById(R.id.FLAG_ONE_SHOT);
Button FLAG_NO_CREATE = (Button) findViewById(R.id.FLAG_NO_CREATE);
FLAG_CANCEL_CURRENT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendNo(PendingIntent.FLAG_CANCEL_CURRENT);
}
});
FLAG_UPDATE_CURRENT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendNo(PendingIntent.FLAG_UPDATE_CURRENT);
}
});
FLAG_ONE_SHOT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendNo(PendingIntent.FLAG_ONE_SHOT);
}
});
FLAG_NO_CREATE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendNo(PendingIntent.FLAG_NO_CREATE);
}
});
}
@SuppressLint("NewApi")
private void sendNo(int flags) {
NotificationManager manager = (NotificationManager) this
.getSystemService(this.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(
MainActivity.this);
builder1.setSmallIcon(R.drawable.ic_launcher); // 设置图标
builder1.setTicker("显示第二个通知");// 消息栏弹出来时显示的内容
builder1.setContentTitle("通知"); // 在下拉通知栏中查看通知时,显示的通知的标题
builder1.setContentText(content.getText().toString()); // 在下拉植栏中查看通知时,显示的通知的内容
builder1.setWhen(System.currentTimeMillis()); // 发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); // 设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);// 点击后是否消失
RemoteViews view = new RemoteViews(getPackageName(),
R.layout.button_play);
PendingIntent intent1 = PendingIntent.getActivity(this, Integer
.parseInt(num.getText().toString()), new Intent(this,
ThreeActivity.class).putExtra("send", "开始"), 0);// 自定义布局的意图的flag不能相同,相同则只会以最新的为标准
// 设置未知意图监听
view.setOnClickPendingIntent(R.id.play, intent1);
PendingIntent intent2 = PendingIntent.getActivity(this, Integer
.parseInt(num.getText().toString()), new Intent(this,
ThreeActivity.class).putExtra("send", "停止"), 1);// 自定义布局的意图的flag不能相同,相同则只会以最新的为标准
view.setOnClickPendingIntent(R.id.stop, intent2);// 设置位置意图上的自定义布局上的控件的监听
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
PendingIntent pendingIntent = null;
if (box.isChecked()) {
pendingIntent = PendingIntent.getActivity(MainActivity.this,
Integer.parseInt(num.getText().toString()), intent, flags);
} else {
pendingIntent = PendingIntent.getActivity(MainActivity.this, 1,
intent, flags);
}
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
// notification1.contentView = view;
if (box1.isChecked()) {
manager.notify(Integer.parseInt(num.getText().toString()),
notification1); // 通过通知管理器发送通知
} else {
manager.notify(1, notification1); // 通过通知管理器发送通知
}
}
PendingIntent的flag相关推荐
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu;import android.os.Bundle; import android.view.View; import android.v ...
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification: Java代码 private Notification genreNotification(Contex ...
- Android开发之自定义Notification(源代码分享)
Android的自定义notification选项我觉得限制还是挺多的,如图所示,官方API告诉我们它依然必须得设置icon,tittle,text三个选项,除此之外,还一定要设置pengdingin ...
- Android开发通知栏的那些事
对于通知栏的使用,Android各个版本其实都有比较大的调整.例如老版本的不兼容,大小图标问题以及自定义通知栏适配问题,这些都是比较头大的事,当然弄懂了就清楚了,本篇就处理下这些疑惑. 通知栏的使用 ...
- android remoteviews 设置背景,理解RemoteViews
理解RemoteViews 什么是remoteViews?按照字面意思是远程View,但RemoteViews并没有继承View,而是继承Object.它的作用是可以跨进程更新界面,听起来有点神奇.R ...
- GCM Google官方示例的简单介绍和使用
GCM Google官方示例的简单介绍和使用 准备工作 翻墙 先翻墙,翻不了墙一切都白搭-- Google账号 申请Google账号 进入Google开发管理台 创建工程(Google管理台上的Pro ...
- Android 12的行为变更和版本兼容思路
一年一度的产品线兼容活动又开始了.Android系统每更新一次系统,对开发者而言都是持续而漫长的挑战. 自15年6.0的兼容以来,每年给公司产品线app做版本兼容,成了我每年的保留节目-- 结束完产品 ...
- Android Target 从 30 升到 31
背景:11月1号起,上传aab 到 google play 必须是target 31,然后产品被拒,那只能升级了. 步骤如下: 1.从主工程target 从30升到31 2.位置信息: 用户可以请求应 ...
- Android学习--深入探索RemoteViews
什么是RemoteViews RemoteViews表示的是一个View结构,它可以在其他进程中显示,由于它在其他进程中显示,为了能够及时更新它的界面,RemoteViews提供了一组基础的操作来跨进 ...
最新文章
- linux 读取磁盘扇区,linux 下检查硬盘坏道/扇区
- 思科学习空间(思科官方网站)
- 最新款服务器主板,服务器主板
- nginx最大并发连接数的思考:worker_processes、worker_connections、worker_rlimit_nofile
- openwrt使用linux内核版本,降低OpenWRT的Linux内核版本
- idea查看类关系_IDEA查看类关系图【正确姿势】,非常实用
- P5704 【深基2.例6】字母转换(python实现)
- 链表冒泡排序java_055-冒泡排序算法代码实现
- CentOS 常用命令
- 《深度学习笔记》——初识PyTorch的学习笔记
- HTML编程之利用jquery制作长图动画的方法
- 使用sharding-jdbc实现水平分表
- linux内核异常解析,内核异常分析方法
- 1、SVPWM空间矢量脉宽调制和基本原理
- python中颜色表_python 颜色表
- TOPSIS法——利用原始数据进行综合评价
- UVM--单向通信、双向通信、多向通信和通信管道
- disallow php,在robots.txt中Disallow: /abc和Disallow: /abc/的区别
- 分油问题回朔法c语言算法,用回溯法求“韩信分油”问题所有解
- wordpress.mu
热门文章
- matlab怎么产生一个随机数,matlab怎么产生随机数
- GSM劫持+短信嗅探是什么,如何防范指南
- iphone传输 android,安卓苹果数据互传,三种方法教你怎样把iPhone数据迁移到安卓手机...
- 金笛JDMAIL邮件服务器证券行业邮件归档解决方案
- 北京奖励专精特新企业补贴有哪些及怎么建设重点介绍,补贴20-50万
- 微信之父张小龙最新公开课曝光8个互联网新思维:下一个十年将非常有想象力...
- VMware ESXi 8.0 Unlocker OEM BIOS 集成网卡驱动和 NVMe 驱动 (集成驱动版)
- pcapy安装教程(多图,超详细)
- 知识图谱论文阅读(十三)【2020 arXiv】Attentive Knowledge Graph Embedding for Personalized Recommendation
- 圣诞树小程序,动态效果,八个小程序