通知栏开发与适配Android8.0

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/denglusha737/article/details/77915472

1.Android通知栏在项目开发中,还是用的比较多的,一般是这三个步骤来做Android通知栏:

    (1)获取通知栏管理器对象

   NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   (2)创建通知对象,并设置ui
  NotificationCompat.Builder builder = new NotificationCompat.Builder(GlobalApplication.globalContext);
  builder.setSmallIcon(R.drawable.smalleIcon);  
  builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.largeIcon));
  builder.setContentTitle(title);   //通知栏标题
  builder.setContentText(progress + "%");     //通知栏内容

 builder.setTicker("新消息"); //显示在状态栏
  Notification notification = builder.build(); 
   (3)发送通知
  mNotificationManager.notify(id,notification);

2.通知栏的其他api

   (1)通知栏的移除:mNotificationManager.cancle(id);        //该id需要同发送通知的id一致

   (2)点击事件处理:

    // 构建一个Intent  
    Intent resultIntent = new Intent(NotificationActivity.this,  TestActivity.class);  
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK  
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
  
      // 封装一个PendingIntent  
     PendingIntent resultPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, resultIntent,  PendingIntent.FLAG_UPDATE_CURRENT);  
     // 设置通知主题的意图  
      builder.setContentIntent(resultPendingIntent);  

3.注意问题:

 (1)使用系统样式必须设置三个属性,否则崩溃。

  setContentText()
  setContentTitle()
  setSmallIcon()
  (2)下载更新进度时,必须控制更新频率(或者不设置largeIcon),否则部分手机(nexus)会导致内存溢出崩溃。我们可以定时更新,这样进度会跳跃式前进,市场上大部分都是这么做的。 
    private NotificationCompat.Builder getNotification(String title, int progress) {
        //设置通知的标题
        builder.setContentTitle(title);
        if (progress >= 0) {
            //当progress大于或等于0时,才需要显示下载进度
            MyLogUtil.i(TAG + "---getNotification--progress:" + progress);
            builder.setContentText(progress + "%");
            builder.setProgress(100, progress, false);
        }
        return builder;
    }
  (3)自定义通知栏样式时,问题更多,且适配是个问题(背景色各不一致),一般还是不建议这么做了。
   在使用remoteView时,layout需要注意控件的使用(只能是AnalogClock,Button,Chronometer,ImageButton,mageView,ProgressBar,TextView这7种),且宽度必须为0,wrap_content,或match_parent,fill_parent不可用。


扫描二维码关注公众号,回复: 4426497 查看本文章

4.适配Android8.0

(1) android 8.0中通知栏必须设置channel,否则无法展示。

         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(false); //是否在桌面icon右上角展示小红点
            channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
            channel.enableVibration(false);
            channel.setSound(null, null);
            mNotificationManager.createNotificationChannel(channel);
        }
 builder = new NotificationCompat.Builder(context, channelId);
channelId为整数字符串,channelName最好使用汉字,因为通知栏上右滑出现设置按钮,点开设置按钮,可以看到这个通知

     channalName.


(2)状态栏图标问题

  builder.setSmallIcon(R.drawable.smalleIcon);  可以设置状态栏图标。但是该图标有一定要求,除了主干部分,其他地方需要透明,否则在显示时会是一个深灰色的矩形。

   


猜你喜欢

转载自blog.csdn.net/denglusha737/article/details/77915472