PendingIntent中第四个参数flag:
目前有这几种flag:FLAG_NO_CREATE;FLAG_CANCEL_CURRENT;FLAG_ONE_SHOT;FLAG_UPDATE_CURRENT
最常用的是最后一种FLAG_UPDATE_CURRENT
个人理解(通俗易懂):
FLAG_NO_CREATE:很好理解,字面意思(没有创建)PendingIntent没有作用,对消息而言没有点击效果。说白了,就是点了没反应。
FLAG_ONE_SHOT:也很好理解,字面意思“只打一枪”,多明显,就是只能点一次,然后你不调用NotificationManager的cancel()方法的取消通知,back再点击通知也进不去,因为只能点一次,怎么样,很好理解吧。(PS:别想着改成FLAG_TWO_SHOT,FLAG_THREE_SHOT,并没有什么卵用,flag只有4种。)
FLAG_CANCEL_CURRENT 和FLAG_UPDATE_CURRENT相对来说比较复杂一点,但是也很好理解。
我们设置两个通知
manager.notify(1,notificataion);
manager2.notify(2,notificataion2);
这两个通知Intent的同一个PendingIntent,按顺序依次释放通知,如果两个flag都设置为FLAG_CANCEL_CURRENT,我们点击第一个通知,会发现,没有反应,就跟FLAG_NO_CREATE一样,但是我们此时点击第二个通知,即会进入Intent。我们可以理解为,第一个通知的点击效果被第二个通知清除了,我们只能通过最新的一条通知进去。但是,我们如果将两条flag都设置为FLAG_UPDATE_CURRENT,这时候,我们会发现,不过管是第一条还是第二条通知,都会进入Intent跳转。
测试代码如下:
MainActivity代码:
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button sendNotice;
private Button sendNotice2;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice=(Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
sendNotice2=(Button) findViewById(R.id.send_notice2);
sendNotice2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.send_notice:
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notificataion=new Notification(R.drawable.ic_launcher, "百度给你发了个推送",System.currentTimeMillis());
Intent intent=new Intent(this,NotificationActivity.class);
PendingIntent p=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_NO_CREATE);
notificataion.setLatestEventInfo(this, "百度推送", "百度新闻", p);
manager.notify(1,notificataion);
break;
case R.id.send_notice2:
NotificationManager manager2=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notificataion2=new Notification(R.drawable.ic_launcher, "QQ有人抖了你一下",System.currentTimeMillis());
Intent intent2=new Intent(this,NotificationActivity.class);
PendingIntent p2=PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);
notificataion2.setLatestEventInfo(this, "QQ通知", "有人给你发信息了", p2);
manager2.notify(2,notificataion2);
break;
default:
break;
}
}
}
NotificationActivity:
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
}
}
layout布局文件代码:
activit_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="百度推送"
/>
<Button
android:id="@+id/send_notice2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ通知"
/>
</LinearLayout>
notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="这是一条通知"
/>
</RelativeLayout>
最后别忘了修改AndroidManifest.xml中代码,加入NotificationActivity的注册声明:
<activity android:name=".NotificationActivity" >
</activity>
这几种flag的定义(可能比较难理解):
FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
利用FLAG_NO_CREAT获取的PendingIntent,若描述的Intent不存在则返回NULL值.
FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
利用 FLAG_ONE_SHOT获取的PendingIntent只能使用一次,即使再次利用其他方法重新获取,再使用PendingIntent也将失败。
FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的。你可用使用它去检索新的Intent,如果你只是想改变Intent中的额外数据的话。通过取消先前的Intent,可用确保只有最新的实体可用启动它。如果这一保证不是问题,考虑flag_update_current。
FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don’t care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有 更新的时候需要用到这个flag去更新你的描述,否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。
(PS:若无法理解,可以参考上面的个人理解。)