安卓 自定义Notification

本文是关于如何创建一个自定义界面的Notification,如图:

这里写图片描述

其实这是一个包含BigContentView样式的通知;这种通知比普通通知多出了一个自定义的BigContentView;
弹出通知时,双指下拉通知即可展示出设置的BigContentView.

直接代码解释:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static void createNotification(Context context) {
        //1.创建RemoteViews
        mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget_no_config);
            //mRemoteViews.addView(R.id.ll_parent,remoteView3);
        //2.构建一个打开Activity的PendingIntent
        Intent intent=new Intent(context,MainActivity.class);
        PendingIntent mPendingIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //3.创建一个Notification
        //兼容类:NotificationCompat:Helper for accessing features in Notification introduced after API level 4 in a backwards compatible fashion.
        mNotification = new NotificationCompat.Builder(context)
                //使用自定义布局的通知在EMUI 上 ticker和smallIcon能显示正常,MIUI则不行(MIUI重要通知以Heads-up形式来展示的)
                .setSmallIcon(R.mipmap.desk1)
                .setTicker("ticker")

                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setContentTitle("contentTitle")
                .setContentText("contentText")

                .setAutoCancel(false) //通知点击之后是自动取消

                //设置通知的大号布局;所谓大号布局是 对通知双指下拉所展示的布局
                //api 16 添加的;给通知设置自定义的布局
                //等同于 mNotification.bigContentView = mRemoteViews;
                .setCustomBigContentView(mRemoteViews)

                //使用自定义的通知布局那么Style不起作用
                //.setStyle(new NotificationCompat.BigPictureStyle())

                .setUsesChronometer(true) //通知右侧会显示一个计时器,显示通知的弹出计时
                .setContentIntent(mPendingIntent)
                .setOnlyAlertOnce(true)  //设置相同通知的声音 震动,ticker 只显示一次
                .setOngoing(true)  //设置通知不会被 通知栏的 "X"清除通知按钮导致消失掉;并且是显示在别的通知之上; 华为手机左侧会标志通知:运行中
                .build();
        // mNotification.contentView = mRemoteViews;         // 不能添加这一句话!
        mNotification.priority = Notification.PRIORITY_MAX;  //优先级:直接在通知栏的最上方显示出来
       //4.获取NotificationManager,生成通知
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(123, mNotification);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值