notification是一种出现在任务栏的提示,
特别是在4.0以后notification改进了不少,
本文内容都是基于4.0及4.1以后总结来的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
btn=(Button) findViewById(R.id.btn);
showNotification(this);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
clearNotification();
}
});
}
public static void showNotification(Context context) {
icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.img1);
//---------------------------------普通的-------------------------
// TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// // Adds the back stack
// stackBuilder.addParentStack(MainActivity1.class);
// // Adds the Intent to the top of the stack
// Intent resultIntent = new Intent(context, context.getClass());
// stackBuilder.addNextIntent(resultIntent);
// // Gets a PendingIntent containing the entire back stack
// PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
// PendingIntent.FLAG_UPDATE_CURRENT);
//
//
// Notification notification = new NotificationCompat.Builder(context)
// .setLargeIcon(icon)
// .setSmallIcon(R.drawable.ic_launcher).setTicker("showNormal")
// .setContentInfo("contentInfo").setContentTitle("ContentTitle")
// .setContentText("ContentText").setNumber(messageNum)
// .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
// .build();
//
// manager.notify(NOTIFICATION_ID, notification);
// //-------------------------大佈局的------------------------------------
// NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
// textStyle
// .setBigContentTitle("BigContentTitle")
// .setSummaryText("SummaryText")
// .bigText(
// "I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
// Notification notification = new NotificationCompat.Builder(context)
// .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
// .setTicker("showBigView_Text").setContentInfo("contentInfo")
// .setContentTitle("ContentTitle").setContentText("ContentText")
// .setStyle(textStyle)
// .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
// .build();
// manager.notify(NOTIFICATION_ID, notification);
// ---------------------- //大布局picture类型--------------------------
// NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
// pictureStyle.setBigContentTitle("BigContentTitle")
// .setSummaryText("SummaryText").bigPicture(icon);
// Notification notification = new NotificationCompat.Builder(context)
// .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
// .setTicker("showBigView_Pic").setContentInfo("contentInfo")
// .setContentTitle("ContentTitle").setContentText("ContentText")
// .setStyle(pictureStyle)
// .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
// .build();
// manager.notify(NOTIFICATION_ID, notification);
// //----------------------大布局Inbox类型-----------------------------
// NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
// "SummaryText");
// for (int i = 0; i < 5; i++)
// inboxStyle.addLine("news:" + i);
// Notification notification = new NotificationCompat.Builder(context)
// .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
// .setTicker("showBigView_Inbox").setContentInfo("contentInfo")
// .setContentTitle("ContentTitle").setContentText("ContentText")
// .setStyle(inboxStyle)
// .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
// .build();
// manager.notify(NOTIFICATION_ID, notification);
//-------------------------------------------------------------------------------------
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
stackBuilder.addParentStack(MainActivity1.class);
// Adds the Intent to the top of the stack
Intent resultIntent = new Intent(context, MainActivity1.class);
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
.setTicker("backApp").setContentInfo("contentInfo")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setContentIntent(resultPendingIntent).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL).build();
manager.notify(NOTIFICATION_ID, notification);
}
// 删除通知
private void clearNotification() {
// 启动后删除之前我们定义的通知
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
}
几种样式,,自己试试吧,,新版本的Notification