我正在研究Notification,并且看到了创建通知对象的两种不同方法,希望有人可以帮助解释这两种方法之间的区别?
示例1
Notification myNotification = new NotificationCompat.Builder(this, "CHANNEL_ID").build();
EX.2
NotificationCompat.Builder myNotification = NotificationCompat.Builder(this, "CHANNEL_ID");
答案 0 :(得分:1)
如果您构建了构建器,则这两行最终是相同的
将构建器保留为变量的一个原因是,是否需要有条件地在其上设置值
NotificationCompat.Builder myNotificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID");
if (foo) myNotificationBuilder.setBar("xyz"); // for example
Notification n = myNotificationBuilder.build();