【发布时间】:2020-05-22 04:18:38
【问题描述】:
我使用自定义布局的自定义音频播放器通知。
simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.custom_notification);
expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.big_notification);
并创建通知
notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("KJV Bible")
//.setOngoing(false)
.setAutoCancel(true)
.setDeleteIntent(createOnDismissedIntent(this))
.setChannelId(CHANNEL_ID).build();
并添加自定义视图的监听器
public void setListeners(RemoteViews view) {
Intent previous = new Intent(NOTIFY_PREVIOUS);
Intent delete = new Intent(NOTIFY_DELETE);
Intent pause = new Intent(NOTIFY_PAUSE);
Intent next = new Intent(NOTIFY_NEXT);
Intent play = new Intent(NOTIFY_PLAY);
mMyBroadcastReceiver = new NotificationBroadcast(controls);
IntentFilter filterPrevious = new IntentFilter(NOTIFY_PREVIOUS);
this.registerReceiver(mMyBroadcastReceiver, filterPrevious);
IntentFilter filterDelete = new IntentFilter(NOTIFY_DELETE);
this.registerReceiver(mMyBroadcastReceiver, filterDelete);
IntentFilter filterPause = new IntentFilter(NOTIFY_PAUSE);
this.registerReceiver(mMyBroadcastReceiver, filterPause);
IntentFilter filterNext = new IntentFilter(NOTIFY_NEXT);
this.registerReceiver(mMyBroadcastReceiver, filterNext);
IntentFilter filterPlay = new IntentFilter(NOTIFY_PLAY);
this.registerReceiver(mMyBroadcastReceiver, filterPlay);
PendingIntent pPrevious = PendingIntent.getBroadcast(getApplicationContext(), 0, previous, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent pDelete = PendingIntent.getBroadcast(getApplicationContext(), 0, delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent pPause = PendingIntent.getBroadcast(getApplicationContext(), 0, pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent pNext = PendingIntent.getBroadcast(getApplicationContext(), 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
PendingIntent pPlay = PendingIntent.getBroadcast(getApplicationContext(), 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
}
现在,当播放器处于播放模式时,当播放器 id 暂停而不打开时,我如何删除或关闭此通知?
我使用了 setDeleteIntent() 但效果不佳。
private PendingIntent createOnDismissedIntent(Context context) {
Intent intent = new Intent(context, NotificationDismissedReceiver.class);
intent.putExtra("notificationId", NOTIFICATION_ID);
return PendingIntent.getBroadcast(context.getApplicationContext(), NOTIFICATION_ID, intent, 0);
return PendingIntent.getBroadcast(this, 0, new Intent(NOTIFY_DELETE), PendingIntent.FLAG_UPDATE_CURRENT);
}
【问题讨论】:
-
setOngoing(false)应该这样做。 -
正在尝试但无法正常工作,请对此发表评论。
标签: android notifications dismiss