知识 | 解析android知识点基础

Android通知栏详解

2017-01-25  本文已影响6528人  骑着海去看蜗牛
华为P9上的通知栏截图

系统默认的通知风格

可以看到,Android通知栏默认是标题显示一行,内容显示一行,对于一行显示不完的,用省略号代替。

private void showNotification(Context context, int id, String title, String text) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setAutoCancel(true);
        builder.setOnlyAlertOnce(true);
        // 需要VIBRATE权限
        builder.setDefaults(Notification.DEFAULT_VIBRATE);
        builder.setPriority(Notification.PRIORITY_DEFAULT);

        // Creates an explicit intent for an Activity in your app
        //自定义打开的界面
        Intent resultIntent = new Intent(context, FlashPageActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
              resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        NotificationManager notificationManager = (NotificationManager) context
              .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, builder.build());
    }

这是常见的通知栏的样式,不再赘述。
在不同的手机上,只设置builder.setSmallIcon(R.drawable.ic_launcher)表现可能不一致,在华为P9上不会显示大的icon,只会显示小的icon。但是在魅族上却显示了,显示的应该是应用的默认图标。

关于setIcon

所以建议还是设置

builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources()
                    ,R.drawable.ic_launcher));

内容显示多行的处理

        // Big views were introduced in Android 4.1, and they're not supported on older devices.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)
                   .setBigContentTitle(title));
        }

显示大图

与上面的显示多行文本类似,也是在Android4.1中官方推出的Notification.BigPictureStyle。
使用方式也相同

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setStyle(new Notification.BigPictureStyle()
                   .bigPicture(aBigBitmap));
        }

自定义通知栏

      builder.setSmallIcon(R.mipmap.ic_icon);
      builder.setContent(remoteViews);
      builder.setContentIntent(contentIntent);

是因为在用户还没有划出通知栏的时候需要提醒用户收到通知,所以需要显示smallIcon。

private void showRemoteView() {
        Intent intent = new Intent(this, MaxBoxActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.layout_custom_notifycation);
        remoteViews.setImageViewResource(R.id.iv_icon, R.mipmap.ic_icon);
        remoteViews.setTextViewText(R.id.tv_title, "我是一个有内涵的程序猿");
        remoteViews.setTextViewText(R.id.tv_description,
                "你懂我的,作为新时代的程序员,要什么都会,上刀山下火海,背砖搬砖都要会,不然哈哈哈哈哈哈哈哈哈你就惨了");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_icon);
        builder.setContent(remoteViews);
        builder.setContentIntent(contentIntent);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            builder.setCustomBigContentView(remoteViews);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        notificationManager.notify(111, builder.build());
    }
华为手机上的表现
可以看到在小米上大图模式是不支持的,华为手机是支持的,并且左右两边都有一个系统设置的padding或者margin值,很难看,华为左边也有一个padding值,所以建议自定义布局的icon不要设置左右的margin值。
// 5.0及以上
android:textAppearance="@android:style/TextAppearance.Material.Notification.Info"
// 5.0以下
android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Info"
上一篇 下一篇

猜你喜欢

热点阅读