Android:在活动开始时更改视图可见性|在通知按钮上单击隐藏通知面板



我的应用程序有一个Notification,其中包含一个Button

当用户点击Button时,应用程序应该打开(如果它是关闭的)并显示一个自定义对话框,这是我创建的一堆视图。

显示对话框意味着将其Visibility设置为View.VISIBLE

我使用BroadcastReceiver来接收NotificationButton点击,它工作(我使用Toast消息来检查它,它确实-当Button被点击时,Receiver被调用,并且它达到了将对话框Visibility更改为View.VISIBLE的功能)

当我点击Button时,应用程序打开,但对话框不会显示。

另外,我注意到,当我点击Button时,Notification面板不隐藏(不像点击Notification本身),我想也许它与它有关。

所以我找到了这个答案,但它不适合我(下面有另一个答案,更新得多,但我不明白他在那里做了什么,也不明白我该怎么做)。

我也用Toast消息检查了对话框的Visibility参数是否在改变,它正在改变为View.VISIBLE(0),仍然没有显示对话框。
如果我在应用程序中并试图显示对话框,它工作,对话框显示在应用程序内按钮点击,问题发生时,试图显示对话框,只要打开应用程序使用NotificationsButton点击

Notification Panel的问题?
如果是,为什么会发生这种情况,我如何隐藏通知面板?
如果不是,是什么,我该如何解决这个问题?

编辑

我被要求提供初始化和显示通知的代码,所以它是:

@RequiresApi(api = Build.VERSION_CODES.M)
private void updateNotification() {
isBatteryOptimizationActive = App.isBatteryOptimizationActive();
clearAllTimerNotificationActionButtons();
if (isBatteryOptimizationActive) {
initBatteryOptimizationActiveNotification();
} else {
if (notificationJustStarted) {
initNotificationBase();
}
initAndStartPomodoroTimerNotification();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NotificationManager manager = getSystemService(NotificationManager.class);
manager.notify(TIMER_NOTIFICATION_ID, pomodoroNotificationBuilder.build());
}
}
private void initNotificationBase() {
pomodoroNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
pomodoroNotificationBuilder
.setContentTitle(getString(R.string.timer_notification_background_allowed_title))
.setOngoing(true)
.setAutoCancel(false)
.setSmallIcon(R.drawable.tomato)
.setContentIntent(pendingIntent)
.setNotificationSilent()
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationJustStarted = false;
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void initAndStartTimerNotification() {
String enableScreenOverlayText = "",
actionButtonTitle;
PendingIntent pendingIntent;
if (!Settings.canDrawOverlays(this)) {
/** Initialize the notification in case "draw over other apps" setting is disabled */
Intent notificationIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
actionButtonTitle = "Enable";
enableScreenOverlayText = String.format("To be able stopping the timer from the notification:nClick Enable -> Search for %s -> Allow Display over other apps", getString(R.string.app_name));
} else {
/** Initialize the notification in case "draw over other apps" setting is enabled */
Intent stopActionIntent = new Intent(this, PomodoroService.class);
stopActionIntent.setAction(ACTION_STOP_SERVICE);
stopActionIntent.putExtra(POMODORO_SERVICE_STOP_FOR_REAL_EXTRA_NAME, false);
pendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
actionButtonTitle = "Stop";
}

pomodoroNotificationBuilder
.setContentText(String.format("%s is in progressnThis session remains: %s until finished", goalName, PublicMethods.formatStopWatchTime(millisRemaining)))
.setStyle(new NotificationCompat.BigTextStyle().bigText(enableScreenOverlayText))
.addAction(R.drawable.stop, actionButtonTitle, pendingIntent);
}

如何隐藏通知面板?

我看到您正在接收接收器中的操作按钮单击。所以,忽略它就变得很容易了。在收件人中,添加以下一行:

NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.cancel(TIMER_NOTIFICATION_ID);

当我点击按钮时,应用程序打开,但对话框不会显示。

你需要传递一个额外的意图,你想要显示对话框。然后,您可以在onCreate()中获得额外的内容,然后相应地显示对话框。

最新更新