Android NotificationCompat通知栏

第一步:导入v4的包第二步:NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder notificationCompat=new NotificationCompat.Builder(...
摘要由CSDN通过智能技术生成

简介

  • 这是一种具有全局效果的通知
  • 最典型的例子就是QQ了
  • 一般比较重要的通知,我们都是使用这个来通知用户的

使用

记得要导入包哦!

compile 'com.android.support:support-v4:+'

实例:

public class MainActivity extends AppCompatActivity 
{
   
   private NotificationManager notificationManager;
   private NotificationCompat.Builder notificationCompat;
   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
   
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationCompat=new NotificationCompat.Builder(this);
      notificationCompat.setContentText("内容");
      notificationCompat.setContentTitle("标题");
      notificationCompat.setSmallIcon(R.drawable.ic_launcher);
      
      findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
   
         @Override
         public void onClick(View p1)
         {
   
            notificationManager.notify(1, notificationCompat.build());
         }
      });
   }
}

效果图:
在这里插入图片描述

怎么回事?怎么和QQ的效果不一样呢?别着急,我们继续往下看,在里面再添加这两段代码,然后我们再试试!

notificationCompat.setPriority(NotificationCompat.PRIORITY_MAX);
notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);

效果图:
在这里插入图片描述

嘿嘿,已经可以了,接下来,让我们为它设置一个点击事件吧!

  • Notification不仅仅能够对用户进行通知,还能够与用户发生互动
  • 我们可以通过PendingIntent为我们的Notification设置点击事件
  • PendingIntent可选的主要有getActivity、getBroadcast、getService

添加下面的两段代码

Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.baidu.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,in,0);
//为Notification整体设置一个点击事件
notificationCompat.setContentIntent(pendingIntent);

完整代码:

public class 
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值