PendingIntent的flag

来源:互联网 发布:如何评价兄弟连 知乎 编辑:程序博客网 时间:2024/05/22 02:13

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); // 通过通知管理器发送通知
}
}





0 0
原创粉丝点击