Notification通知栏

通知栏可以设置很多样式,根据不同的需求可以设置不同的通知栏:

1 最普通的一种通知栏:

	private void initNotification() {
		Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("htt://www.baidu.com"));
		PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, 0);
		
		Notification.Builder builder= new Notification.Builder(MainActivity.this);
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setContentIntent(pendingIntent);
		builder.setAutoCancel(true);
		builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
		builder.setContentTitle("这是标题");
		builder.setContentText("这是内容");
		builder.setSubText("这是内容下面的文字");
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = builder.build();
		notificationManager.notify(NOTIFICATION_ID_NUM1,notification);
		
	}


2.设置一个折叠式的通知栏:

private void initNotification2() {
		
		Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("htt://www.sina.com"));
		PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, 0);
		
		Notification.Builder builder= new Notification.Builder(MainActivity.this);
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setContentIntent(pendingIntent);
		builder.setAutoCancel(true);
		builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = builder.build();

              <strong>  notification.contentView =remoteViews;
		notification.bigContentView = extendViews;</strong>
		
		notificationManager.notify(NOTIFICATION_ID_NUM2,notification);
	}

两种布局:可以自定义

	remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
		remoteViews.setTextViewText(R.id.tv_no, "我是内容");
		
		extendViews = new RemoteViews(getPackageName(),R.layout.extendview);
		extendViews.setTextViewText(R.id.tv_no, "我是内容");

3设置悬挂式的通知栏;

private void initNotification3() {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setClass(this, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, PendingIntent.FLAG_CANCEL_CURRENT);
	
		Notification.Builder builder= new Notification.Builder(MainActivity.this);
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setPriority(Notification.PRIORITY_DEFAULT);
		builder.setCategory(Notification.CATEGORY_MESSAGE);
		builder.setContentTitle("这是标题");
		builder.setContentText("这是内容");

          <strong>      builder.setFullScreenIntent(pendingIntent, true);</strong>
		
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = builder.build();
		notificationManager.notify(NOTIFICATION_ID_NUM3,notification);
	}


4通知栏分等级:

     * Notification.VISIBILITY_PUBLIC:没有锁屏的时候会显示
     * Notification.VISIBILITY_PRIVATE:任何情况都会显示
     * Notification.VISIBILITY_SECRET:在pin password 安全锁和没有锁屏的情况才会显示。

private void initNotification4() {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setClass(this, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, PendingIntent.FLAG_CANCEL_CURRENT);
	
		Notification.Builder builder= new Notification.Builder(MainActivity.this);
		<strong>builder.setVisibility(Notification.VISIBILITY_SECRET);</strong>
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setPriority(Notification.PRIORITY_DEFAULT);
		builder.setCategory(Notification.CATEGORY_MESSAGE);
		builder.setContentTitle("这是标题");
		builder.setContentText("这是内容");
		builder.setFullScreenIntent(pendingIntent, true);
		
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = builder.build();
		notificationManager.notify(NOTIFICATION_ID_NUM3,notification);
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值