android 創建通知欄Notification

		///// 第一步:獲取NotificationManager
		NotificationManager nm = (NotificationManager) 
				getSystemService(Context.NOTIFICATION_SERVICE);

		///// 第二步:定義Notification
		Intent intent = new Intent(this, OtherActivity.class);
		//PendingIntent是待執行的Intent
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);
		Notification notification = new Notification.Builder(this)
				.setContentTitle("title")
				.setContentText("text")
				.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi)
				.build();
		notification.flags = Notification.FLAG_NO_CLEAR;
		
		/////第三步:啓動通知欄,第一個參數是一個通知的唯一標識
		nm.notify(0, notification);
		
		//關閉通知
		//nm.cancel(0);


更復雜的功能可以查詢相關api文檔。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章