第一种:通用通知使用方法,但已被抛弃
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 创建延迟意图 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationTwoActivity.class), 0); // 通用通知使用方法,但已被抛弃 Notification notification = new Notification(); // 当有通知时,屏幕最上方自动弹出的提示语,2秒后自动消失 notification.tickerText = "有新消息,点我"; // 该设置不起效果,通知任然是ic_launcher图标,但如果不设置该属性通知不起作用 notification.icon = R.drawable.aaa; // 该设置不起效果, notification.when = System.currentTimeMillis(); notification.setLatestEventInfo(this, "title", "contentText", pendingIntent); // 该设置不起效果, notification.number = 1; notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知 // 通过通知管理器来发起通知 manager.notify(NOTIFICATION_FLAG, notification); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 清除id为NOTIFICATION_FLAG的通知 manager.cancel(NOTIFICATION_FLAG); // 清除所有通知 // manager.cancelAll(); } }); }
第二种:API11及之后可用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建延迟意图 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationTwoActivity.class), 0); //API 11及之后可用 Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.aaa) .setTicker("有新消息,点我") .setContentTitle("title") .setContentText("contentText") .setNumber(1) .setContentIntent(pendingIntent) // 注意build()是在API level 16及之后增加的,在API11中可以使用getNotificatin()来代替 .getNotification(); notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知 // 通过通知管理器来发起通知 manager.notify(NOTIFICATION_FLAG, notification); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 清除id为NOTIFICATION_FLAG的通知 manager.cancel(NOTIFICATION_FLAG); // 清除所有通知 // manager.cancelAll(); } }); }
第三种:API16及之后可用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建延迟意图 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationTwoActivity.class), 0); // API16及之后可用 Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.aaa) .setTicker("有新消息,点我") .setContentTitle("title") .setContentText("contentText") .setNumber(1) .setContentIntent(pendingIntent) .build(); notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知 // 通过通知管理器来发起通知 manager.notify(NOTIFICATION_FLAG, notification); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 清除id为NOTIFICATION_FLAG的通知 manager.cancel(NOTIFICATION_FLAG); // 清除所有通知 // manager.cancelAll(); } }); }
第四种:自定义通知
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建延迟意图 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationTwoActivity.class), 0); // API16及之后可用 Notification notification = new Notification(); notification.icon = R.drawable.aaa; notification.tickerText = "自定义,有新消息,点我"; notification.when = System.currentTimeMillis(); RemoteViews rv = new RemoteViews(getPackageName(), R.layout.remote); rv.setTextViewText(R.id.tv, "haha,你是我的小苹果"); notification.contentView = rv; notification.contentIntent = pendingIntent; notification.flags = Notification.FLAG_NO_CLEAR; // 点击后不自动清除通知 // 通过通知管理器来发起通知 manager.notify(NOTIFICATION_FLAG, notification); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 清除id为NOTIFICATION_FLAG的通知 manager.cancel(NOTIFICATION_FLAG); // 清除所有通知 // manager.cancelAll(); } }); }
从Android 4.1开始,Notification可以支持展开显示的模式,这样一来,Notification就演变出了下面4种不同的风格样式(builder.setStyle()):
- MediaStyle
- BigPictureStyle
- InboxStyle
- BigTextStyle