消息通知比较常见,主要是用NotificationCompat的一些接口来进行内容设置,有点像AlertDialog的创建,主要是他们的封装都用了Builder;下面给出两段创建和清除消息提示的代码片段。
新建一个消息提示:
public void openNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.action_share) .setContentTitle("Notification Title").setContentText("Hello World!").setNumber(5).setSubText("this is sub content") .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); notificationManager.notify("test", 0, mBuilder.build()); }
关闭消息:
public void cancelNotification() { notificationManager.cancelAll(); }