我将整个过程用函数封装起来,其中参数ord_id :为每个消息的标题,address:为每个消息在任务下拉框显示的内容
public void Notifi(String ord_id,String address){
String d = new SimpleDateFormat("ddHHmmssS").format(Calendar.getInstance().getTime());//这里的参数d为每个消息的唯一标示,由于我设置的是弹出消息为每150ms刷新一次,所以这里我设置d的时候选择精确到一位毫秒 ,保证d不会重复,当然如果连续显示消息一个月 而且不点击查看 的话 就不行了,因为我设置的最大单位为一个月
int i;
try{
i Integer.parseInt(d);
}catch(Exception e){
i = ox1;
}
NotificationManger manger = (NotificationManger)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"短信名称",System.currentTimeMillis());//其中第一个参数为短息提示的图标,后面两个参数是在他之后显示的
Intent intent = new Intent(getApplicationContext(),NewOrder.class);//这里第二个参数设置点击状态栏短信跳转的页面
Bundle data = new Bundle();
data.putString("ord_id",ord_id);
//在这里进行参数的传递
intent.putExtras(data);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);//这里设置多个短信同时打开时,新的短信打开关闭其他已经打开的短信
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),i,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(),ord_id,address,pi);//第二 个参数为短信标题,第三个参数为短信通知内容
notification.defaults = Notification.DEFAULT_ALL;//设置所有短信提醒效果为默认值
notification.flags = Notification.FLAG_AUTO_CANCEL;//消息被点击后,消息自动在通知栏消失
manager.notify(i,notification);//第一个参数为通知唯一id,若重复的覆盖
}