美文网首页
安卓开发Notification前台服务使用

安卓开发Notification前台服务使用

作者: 510bb14393e1 | 来源:发表于2022-05-18 17:20 被阅读0次
private void initView() {
        Notification notification;
//高版本的模拟器或手机还需要开启渠道才能显示通知
        NotificationChannel notificationChannel=null;
        NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            notificationChannel=new NotificationChannel("001","channel_name",NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"001");
//实例化一个意图,当点击通知时会跳转执行这个意图
        Intent intent = new Intent(this, MainActivity.class);
//将意图进行封装
        PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//设置Notification的点击之后执行的意图
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle("酷我音乐");
        builder.setContentText("正在播放的音乐:两只老虎");
        notification = builder.build();
        notificationManager.notify(0,notification);
        //startForeground(0, builder.build());
    }

相关文章

网友评论

      本文标题:安卓开发Notification前台服务使用

      本文链接:https://www.haomeiwen.com/subject/afvaurtx.html