Notification的基本用法

android4.0以前:
private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	private void showNotification(Store store) {
		Notification notification = new Notification();
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults = Notification.DEFAULT_ALL;
		notification.icon = R.drawable.ic_launch;
		notification.when = System.currentTimeMillis();

		Intent intent = new Intent(this,AlarmActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		intent.putExtra("store", store);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT

		 //Change the name of the notification here
		notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);
		notificationManager.notify(NOTIFY_ID, notification);
		

	}


android4.0以后:
private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private void showNotification(Store store) {
		Intent intent = new Intent(this,AlarmActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		intent.putExtra("store", store);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT

		Notification notification = new Notification.Builder(context)         
							.setContentTitle(store.getStoreName()+"("+store.getDistance()+")")
							.setContentText(store.getAddress())
							.setContentIntent(contentIntent)
							.setSmallIcon(R.drawable.ic_launch) 
							.setAutoCancel(true)
							.setWhen(System.currentTimeMillis())
							.setDefaults(Notification.DEFAULT_ALL)
							.getNotification();
		notificationManager.notify(NOTIFY_ID, notification);
		
		//stopSelf();
	}


坑爹的Google API上用法是这样的:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();

我找了半天也没找到build()方法!!!

android Notification 的使用
http://www.cnblogs.com/newcj/archive/2011/03/14/1983782.html

你可能感兴趣的:(android)