【Android】Notification的使用

1.普通的Notification

public void testNormalNotification(View view) {
		//通知管理器用于管理所有通知
		NotificationManager notificationManager = (NotificationManager) getSystemService
				(Context.NOTIFICATION_SERVICE);
		// 设定通知的图标(将会显示在手机顶部已表示此App有通知)、滚动信息的内容和通知时间
		Notification notification = new Notification(R.drawable.app_icon,
				"普通的通知...", System.currentTimeMillis());

		// 点击通知栏后相应的启动组件
		Intent intent = new Intent(mContext, TestLocationActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
				intent, 0);

		// 设置通知的标题和消息内容(打开通知栏后会看到)
		notification.setLatestEventInfo(mContext, "普通通知", "这是普通的通知哦,亲",
				pendingIntent);

		// 通知的ID,在同一个App中要唯一
		final int NOTIFICATION_ID = 0x100;
		notificationManager.notify(NOTIFICATION_ID, notification);
	}

2.新版的Notification

public void testNotificationCompat(View view) {
		// 当用户点击通知栏时启动Intent所指定的组件
		Intent intent = new Intent(mContext, TestSearchHistoryActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		// 用于创建一个Notification
		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				mContext);
		// 设置右下角的小图标,其会现在时屏幕最顶端,用户可以通过此图标辨识是哪个App发来的通知
		builder.setSmallIcon(R.drawable.icon_small_notificaion);
		// 通知的标题
		builder.setContentTitle("通知的标题");
		// 通知正文
		builder.setContentText("通知的正文");
		// 注:以上三个属性是必须设定的

		// 通知栏左侧的大图标
		builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
				R.drawable.app_icon));
		// 通知的子文本
		builder.setSubText("子文本");
		// 通知的消息—— 一般用于表示收到多少个消息
		builder.setContentInfo("通知消息");
		// 通知的右上角中的时间,如果不显示设定,默认是接受到消息的时间
		builder.setWhen(System.currentTimeMillis());
		// 绑定Intent,用户点击时做出相应的动作
		builder.setContentIntent(pendingIntent);

		// 通知是否一致显示在通知栏中,类似QQ用户无法将其从通知栏中删除
		builder.setOngoing(true);
		//点击清理后清除通知
		builder.setAutoCancel(true);
		// 手机上的LED等闪烁色彩,时间,时长
		builder.setLights(0xFF0000, 200, 200);
		// 手机收到通知时振动的样式
		builder.setVibrate(new long[] { 0, 100, 330, 800, });

		// 创建一个通知,并添加灯光闪烁
		Notification notification = builder.build();
		notification.flags = Notification.FLAG_SHOW_LIGHTS;

		// 通知管理器--用于管理各个通知
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		// 将通知交由管理器管理,第二个参数(int)表示该通知的ID,要求其在一个App中唯一
		notificationManager.notify("notificationCompat", 101, notification);
	}

3.自定义的Notification

public void testNotificationByCustom(View view) {
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.app_icon,
				"测试自定义通知", System.currentTimeMillis());

		Intent intent = new Intent(mContext, TestLocationActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
				intent, 1);

		// 自定义Notification的布局
		RemoteViews remoteViews = new RemoteViews(getPackageName(),
				R.layout.notification_test_view);

		remoteViews.setImageViewResource(R.id.bigIcon, R.drawable.app_icon);
		remoteViews.setTextViewText(R.id.tvTitle, "自定义的通知标题");
		remoteViews.setTextViewText(R.id.tvMessage, "亲,第一次看见你(⊙o⊙)哦!");
		remoteViews.setTextViewText(R.id.tvTime, "12:00");
		remoteViews.setImageViewResource(R.id.smallIcon,
				R.drawable.icon_small_notificaion);

		notification.contentView = remoteViews;
		notification.contentIntent = pendingIntent;

		notificationManager.notify(1, notification);
	}


更多参考:

android Notification 的使用

android API--Notification

android Notification Sample




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值