android 添加 notification

来源:互联网 发布:淘宝qq推广 编辑:程序博客网 时间:2024/05/15 21:48

1.定义notification

private NotificationCompat.Builder mBuilder;mBuilder = new NotificationCompat.Builder(MainActivity.this).setSmallIcon(R.drawable.a).setContentTitle("").setContentText("");

2.添加点击notification跳转至activity

private Intent i_notifi;private PendingIntent pi;i_notifi = new Intent(this,MainActivity.class);pi = PendingIntent.getActivity(this, 0, i_notifi, PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.setContentIntent(pi);

3.启动notification

private NotificationManager nm;nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);nm.notify(001, mBuilder.build());

PS:点击notification后自动取消通知栏

mBuilder.setAutoCancel(true);

4.设置notification 显示多文字

Notification notif = new Notification.Builder(mContext)     .setContentTitle("New mail from " + sender.toString())     .setContentText(subject)     .setSmallIcon(R.drawable.new_mail)     .setLargeIcon(aBitmap)     .setStyle(new Notification.BigTextStyle()         .bigText(aVeryLongString))     .build();

参考

0 0