Android 通知(Notification)

/**
	 * 
	 * @param icon 引入图片
	 * @param tickerText 程序在后台运行的时候通知显示提示显示的内容
	 * @param context   上下文
	 * @param contentTitle  通知标题
	 * @param contentText   通知内容
	 * @param id  通知id
	 * @param pendingIntent  通过PendingIntent对象来处理点击通知后的逻辑操作
	 */
	public void  MyNotification(int icon,String tickerText,Context context,String contentTitle,String contentText,int id){
		//获取NotificationManager对象,通过NotificationManager对象进行管理
		NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		//获取Notification对象
		Notification notification = new Notification(icon,tickerText, System.currentTimeMillis());
		//获取Intent对象  点击通知启动com.example.viewpager.PendingIntent活动
		Intent intent = new Intent(getApplicationContext(), com.example.viewpager.PendingIntent.class);
		//获取PendingIntent 对象
		PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		//设置通知的标题和通知文本
		notification.setLatestEventInfo(context, contentTitle, contentText, pi);
		//显示通知   每个通知的id不能重复
		notificationManager.notify(id, notification);
	}

猜你喜欢

转载自a754782339.iteye.com/blog/2264906