通知的使用

通知的使用
调用Context的getSystemService方法获得一个NotificationManager对象manager
调用NotificationCompat.Builder(MainActivity.this,“default”)new一个Notification对象
对这个Notification对象进行一系列操作
调用NotificationManager对象manager的manager.notify(1, notification)方法

下面是显示通知,点击通知跳转另外一个活动的代码

                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);//跳转pendingIntent
                NotificationManager manager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(MainActivity.this,"default")
                        .setContentTitle("这是一个通知标题")
                        .setContentText("这是通知内容")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                        .setContentIntent(pendingIntent)//设置跳转
                        .setAutoCancel(true)//点击后撤销通知
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .build();
                manager.notify(1, notification);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值