Android 5.0后,创建顶部通知栏已经发生改变,样例如下:
//获取NotificationManager实例
NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.icon)
.setContentTitle(title)
.setContentText(description);
Intent resultIntent = new Intent(context, LoginActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(LoginActivity.class);//避免二次页面跳转
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
mBuilder.setContentIntent(resultPendingIntent);// mId allows you to update the notification later on. notifyManager.notify(0, mBuilder.build());