我正在尝试在前台服务通知中添加 3 个按钮,但我遵循的所有指南都失败了
这个答案说要添加操作:https
://stackoverflow.com/a/49539463/5679560 本教程说要使用remoteview 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();
}
}
多一点上下文的问题。我正在尝试创建一个浮动窗口并让服务控制它。一切运行良好,服务启动 -> 进入前台 -> 创建浮动窗口,即使应用程序处于后台也能正常显示
但我想在通知中添加一个关闭按钮,以便用户可以关闭浮动窗口......
顺便说一句,浮动窗口中的文字显示“我的应用正在运行,点击此处查看更多信息”,这绝对不是我为通知写的