0
点赞
收藏
分享

微信扫一扫

【设计模式】建造者模式——建造者模式在Android SDK源码中的应用

建造者模式在Android SDK源码中也有广泛的应用,本文挑两个典型的类讨论一下:

AlertDialog.Builder

在Android源码中最常用到的建造者模式非AlertDialog.Builder莫属,代码如下:

AlertDialog alertDialog = new AlertDialog.Builder(mContext)
		.setTitle("系统提示:")
    .setMessage("请及时查收邮件")
    .setNegativeButton("取消", null)
    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
    		@Override
        public void onClick(DialogInterface dialog, int which) {
        		// 跳转查看邮件页面
        }
     }).create();
alertDialog.show();


NotificationCompat.Builder

出于安全方面的考虑,当前版本Android官方推荐使用构造方法含有channelId的NotificationCompat.Builder来代替Nofication.Builder生成Notification对象,代码如下:

Notification notification = new NotificationCompat.Builder(mContext,"channelId")
		.setContentTitle("New mail from " + sender.toString())
    .setContentText(subject)
    .setSmallIcon(R.drawable.new_mail)
    .setLargeIcon(aBitmap)
    .build();


举报

相关推荐

0 条评论