我正在尝试添加3个按钮到我的前台服务通知,但我遵循的所有指南都失败了
这个答案说要添加操作:https://stackoverflow.com/a/49539463/5679560
本教程介绍了如何使用远程视图https://developer.android.com/training/notify-user/custom-notification
我已经尝试了这两种方法,但我的通知保持默认外观,即使文本根本没有改变,我的通知引用前台服务通知这是问题所在吗?不能在前台服务通知中添加一个小按钮吗?
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this))
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + this.getPackageName())).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
else if (intent.hasExtra(com.tomatedigital.androidutils.Constants.Intent.WINDOW_SERVICE_LAYOUT)) {
new FloatingWindow(this, LayoutInflater.from(this).inflate(intent.getIntExtra(Constants.Intent.WINDOW_SERVICE_LAYOUT, 0), null), "casa");
startForeground(1, Notification.startFloatingWindow(this));
}
return START_REDELIVER_INTENT;
}
public static android.app.Notification startFloatingWindow(@NonNull final Context context) {
@SuppressLint("RemoteViewLayout") RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_floating_window);
//HelperActivity will be shown at step 4
/*
Intent stop = new Intent(context, MeuServico.class);
stop.putExtra("stop", "do");//if necessary
PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
//R.id.radio is a button from the layout which is created at step 2 view.setOnClickPendingIntent(R.id.radio, pRadio);
//Follows exactly my code!
Intent volume = new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
volume.putExtra("DO", "volume");</p >
//HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
view.setOnClickPendingIntent(R.id.volume, pVolume);
*/
return new NotificationCompat.Builder(context, Constants.Notification.FLOATING_WINDOW_NID)
// .setSmallIcon(R.drawable.ic_notification_logo)
.setContentTitle("getString(R.string.foregroundNotification)")
.setContentText("text")
//.setCustomContentView(contentView)
.addAction(R.drawable.ic_stop, "teste", null)
.setPriority(NotificationCompat.PRIORITY_LOW).build();
}
}更多关于这个问题的上下文。我正在尝试创建一个浮动窗口,并让一个服务来控制它。一切运行正常,服务启动->转到前台->创建浮动窗口,即使在应用程序处于后台时也能正常显示
但我想在通知中添加一个关闭按钮,这样用户就可以关闭浮动窗口...
顺便说一下,浮动窗口中的文本显示“我的应用程序正在运行点击此处查看更多信息”,这绝对不是我为通知所写的内容

发布于 2021-10-20 23:28:47
显示如下通知:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL);
builder.setContentTitle("Alarm for: " + (is24 ? _24H : _12H) + (isSnoozeAction ? " (Snoozed)" : ""))
.setContentText(alarmLabel)
.setSmallIcon(R.drawable.ic_n_alarm)
.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.setContentIntent(pi)
.addAction(new NotificationCompat.Action(R.drawable.ic_n_snooze, "Snooze", actionSnooze))
.addAction(new NotificationCompat.Action(R.drawable.ic_n_cancel, "Dismiss", actionDismiss));
manager.notify(NOTIFICATION_ID, builder.build());addAction将一个按钮添加到通知中,传递的第三个参数是一个PendingIntent,它表示实际的操作。
PendingIntent actionDismiss = PendingIntent.getBroadcast(context, 113, receiverDismiss,
PendingIntent.FLAG_ONE_SHOT);
PendingIntent actionSnooze = PendingIntent.getBroadcast(context, 114, receiverSnooze,
PendingIntent.FLAG_ONE_SHOT);https://stackoverflow.com/questions/69651537
复制相似问题