Android NotificationCompat通知栏

简介

  • 这是一种具有全局效果的通知
  • 最典型的例子就是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());
         }
      });
   }
}

效果图:
Android NotificationCompat通知栏_第1张图片

怎么回事?怎么和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 MainActivity extends AppCompatActivity 
{
   private NotificationManager notificationManager;
   private NotificationCompat.Builder notificationCompat;
   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.baidu.com"));
      PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
      
      notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationCompat=new NotificationCompat.Builder(this);
      notificationCompat.setContentText("内容");
      notificationCompat.setContentTitle("标题");
      notificationCompat.setSmallIcon(R.drawable.ic_launcher);
      notificationCompat.setContentIntent(pendingIntent);
      
      findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
         @Override
         public void onClick(View p1)
         {
            notificationManager.notify(1, notificationCompat.build());
         }
      });
   }
}

效果图
Android NotificationCompat通知栏_第2张图片

通知栏还支持设置一个进度条!

  • 进度条分为两种
  • 确定进度条和不确定进度条:

Android NotificationCompat通知栏_第3张图片
Android NotificationCompat通知栏_第4张图片
实例:

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);
      
      Intent mIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com"));
      PendingIntent mPendingIntent=PendingIntent.getActivity(this, 0, mIntent, 0);
      notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationCompat = new NotificationCompat.Builder(this);
      notificationCompat.setContentText("正在下载中");
      notificationCompat.setContentTitle("MyAPP");
      notificationCompat.setSmallIcon(R.drawable.ic_launcher);
      notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);
      notificationCompat.setPriority(NotificationCompat.PRIORITY_DEFAULT);
      notificationCompat.setContentIntent(mPendingIntent);
      
      findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
         @Override
         public void onClick(View p1)
         {
            //设置进度条为不确定的
            notificationCompat.setProgress(100, 0, true);
            //设置为正在进行时的Notification(用户无法取消这条通知)
            notificationCompat.setOngoing(true);
            //设置内容
            notificationCompat.setContentText("正在准备中");
            //显示这条通知,id为0
            notificationManager.notify(0, notificationCompat.build());
            //让主线程体眠2秒后,再去执行循环
            //模拟网络延迟操作
            Thread.sleep(2000);
            for (int i = 0;i <= 100;i = i + 10)
            {
               //设置进度值
               notificationCompat.setProgress(100, i, false);
               //设置内容
               notificationCompat.setContentText("已下载" + i + "%");
               //显示通知,id为0
               //如果已有一条id为0的通知,则会覆盖上一条id为0的通知,否则就会重新创建一条通知
               notificationManager.notify(0, notificationCompat.build());
               //每次循环结束,先体眠200ms,再执行下次循环
               //模拟下载
               Thread.sleep(200);
            }
            //设置为非正在进行中的通知(用户可以取消这条通知)
            notificationCompat.setOngoing(false);
            //设置内容
            notificationCompat.setContentText("已下载完成");
            //先体眠500ms后,再去显示通知
            //去务必加上这句(不然会出错)
            Thread.sleep(500);
            //显示通知
            notificationManager.notify(0, notificationCompat.build());
            //使用toast提示用户已完成
            Toast.makeText(MainActivity.this, "已下载完成", Toast.LENGTH_LONG).show();
         }
      });
   }
}

效果图:
Android NotificationCompat通知栏_第5张图片
设置进度条方法解析

setProgress(int max, int progress, boolean indeterminate)
参数 解析
max 最大进度值
progress 当前进度值
indeterminate 是否为正在进行中的通知
  • 面对我们的需求,仅仅只有系统默认样式的Notification肯定是无法满足我们的
  • 我们需要自定义Notification的样式,才能设计出更精美的App
  • 那么…接下来就让我们开始学习吧!

自定义通知样式

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);
     
     Intent mIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com"));
     PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, mIntent, 0);
     
     notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     notificationCompat = new NotificationCompat.Builder(this);
     //实例化RemoteViews对象,传入当前应用的包名和自定义的通知样式布局
     RemoteViews mRemoteViews=new RemoteViews(getPackageName(), R.layout.layout_notification);
     //设置imageview的显示的图片
     mRemoteViews.setImageViewResource(R.id.layout_notificationImageView1, R.drawable.ic_launcher);
     //设置按钮的文本
     mRemoteViews.setTextViewText(R.id.layout_notificationButton1, "进入");
     //为按钮设置点击事件
     mRemoteViews.setOnClickPendingIntent(R.id.layout_notificationButton1, pendingIntent);
     //(必须)一般不会调用,只有当我们自定义的通知样式出现问题时,才会调用。
     notificationCompat.setSmallIcon(R.drawable.ic_launcher);
     notificationCompat.setContentText("内容");
     notificationCompat.setContentTitle("标题");
     notificationCompat.setContent(mRemoteViews);
     
     //点击空白(白色)区域触发
     //notificationCompat.setContentIntent(pendingIntent);
     
     findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View p1)
        {
           notificationManager.notify(0, notificationCompat.build());
        }
     });
  }
}

其他方法

  • 点击后自动取消通知
notificationCompat.setAutoCancel(true);
  • 取消一条通知
notificationManager.cancel(通知的id);
  • 取消所有通知
notificationManager.cancelAll();

Android NotificationCompat通知栏_第6张图片

  • 首次出现时会从屏幕上方出现
notificationCompat.setTicker("");
  • 显示一张大图
notificationCompat.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)));

Android NotificationCompat通知栏_第7张图片
本文到此结束

你可能感兴趣的:(组件)