PendingIntent的第四个参数的说明

Intent立即响应; pendingintent 类似发送短信,邮件等

*PendingIntent的第四个参数的说明:
* 常量:
* FLAG_CANCEL_CURRENT 生成一个新的对象
* FLAG_NO_CREATE若不存在,则创建一个新的对象
* FLAG_ONE_SHOT创建的对象只能使用一次
* FLAG_UPDATE_CURRENT已存在则直接使用

``` private void generateForegroundNotification() { Intent intentMainLanding = new Intent(mContext, MainActivity.class); //set pending intent flag PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intentMainLanding, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); Bitmap iconNotification = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); if (mNotificationManager == null) { mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } NotificationChannelGroup group = new NotificationChannelGroup("chats_group", "Chats"); mNotificationManager.createNotificationChannelGroup(group); NotificationChannel notificationChannel = new NotificationChannel("service_channel", "Service Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationChannel.setDescription("Channel for the foreground service"); mNotificationManager.createNotificationChannel(notificationChannel); NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "service_channel") .setSmallIcon(R.drawable.number_one) // Set notification icon here .setContentTitle("Foreground Service") .setContentText("MediaPlayback Service is running") .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_DEFAULT); Notification notification = builder.build(); int mNotificationId = 111; startForeground(mNotificationId, notification); }```private void updateNotification(boolean isPlaying) { // 创建带播放/暂停按钮的通知 Intent playIntent = new Intent(this, AudioService.class); playIntent.setAction(isPlaying ? ACTION_PAUSE : ACTION_START); PendingIntent pendingPlayIntent = PendingIntent.getService( this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("背景音乐服务") .setContentText(isPlaying ? "播放中" : "已暂停") .addAction(isPlaying ? R.drawable.ic_pause : R.drawable.ic_play, isPlaying ? "暂停" : "播放", pendingPlayIntent) .addAction(R.drawable.ic_stop, "停止", PendingIntent.getService(this, 0, new Intent(this, AudioService.class).setAction(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT)); startForeground(NOTIFICATION_ID, builder.build()); } 怎么合在一起?
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值