繁体   English   中英

带按钮的 Android 前台服务通知

[英]Android foreground service notification with buttons

我正在尝试向前台服务通知添加 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();
    }
}

问题的更多背景。 我正在尝试创建一个浮动窗口并有一个服务来控制它。 一切运行正常,服务启动 -> 进入前台 -> 创建浮动窗口,即使应用程序处于后台也能正常显示

但我想在通知中添加一个关闭按钮,以便用户可以关闭浮动窗口...

顺便说一句,浮动窗口中的文字说“我的应用程序正在运行,点击此处查看更多信息”这绝对不是我为通知写的

在此处输入图片说明

显示这样的通知:

  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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM