2015/11/23
int notificationId=1;
...
{
Button btn_send = (Button) this.findViewById(R.id.send_notice);
btn_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"1",System.currentTimeMillis());//"1"为通知栏的内容
Intent intent = new Intent(MainActivity.this,Second_activity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
/*pi为当用户进入下拉页面点击item时的意图,实例化pi时第二个参数一般不用,设置为0即可*/ notification.setLatestEventInfo(MainActivity.this, "title", "content", pi);
notificationManager.notify(notificationId,notification);
//nitify(,);第一个参数唯一,如果不做处理,该通知只能显示一次
notificationId++;
//让通知的id++,这样可以显示多次
}
});
}
效果
当点击tiem进入到第二个页面时,设备顶部的图片通知也不会消失,需要在第二个页面做处理
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
/*cancel()的参数为当时创建notification的id*
2016/05/25更新
RemoteViews 是 通知栏 点击展开的内容
RemoteViews是自定义的,即自定义通知栏!
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "tickerText"; // 手机屏幕顶部出现的文字
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.dialog);
remoteViews.setTextViewText(R.id.msg, "to msg");
PendingIntent pendingIntent2 = PendingIntent.getActivity(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.msg, pendingIntent2);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent2;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, notification);
更新2016年9月19日
使用SDK23版本编译
notification.setLatestEventInfo(MainActivity.this, "title", "content", pi);
将编译不过,找不到该方法。
解决方案:http://blog.csdn.net/BruceHurrican/article/details/50571522