android - 通知彩色操作按钮 Android 10

标签 android android-notifications android-10.0

我正在尝试像这样在通知中为按钮(操作)着色。
enter image description here

到目前为止,这就是我迄今为止所取得的成就。
enter image description here

下面是我正在使用的代码

NotificationService.class

private void showCallNotification(Map<String, String> dataMap) {
        notificationId = (int) (System.currentTimeMillis() % 10000);
        Intent intent = new Intent(getApplicationContext(), ReceiveCallActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setAction(Intent.ACTION_ANSWER)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_ACCEPTED)
                .putExtra("title", dataMap.get("title"))
                .putExtra("action", dataMap.get("action"));

        Intent cancelIntent = new Intent(getApplicationContext(), VideoCallReceiver.class)
                .setAction(AppConstants.INCOMING_CALL_BROADCAST_ACTION)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_DECLINED)
                .putExtra("notificationId", notificationId);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Action declineAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.decline_call), cancelPendingIntent);
        NotificationCompat.Action acceptAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.accept_call), pendingIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(dataMap.get("sender"))
                .setContentText(getString(R.string.incoming_call))
                .setColor(getResources().getColor(R.color.notification_color))
                .setAutoCancel(true)
                .setTimeoutAfter(CALL_DISMISS_TIME)
                .addAction(declineAction)
                .addAction(acceptAction)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setFullScreenIntent(pendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createCallNotificationChannel();
        }

        notificationManager.notify(notificationId, builder.build());
    }

我现在没有主意了。任何帮助将不胜感激。

最佳答案

代码取自Android官方Dialer应用程序

当您设置操作字符串 getString(R.string.decline_call)通过调用方法来改变它

  private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
    Spannable spannable = new SpannableString(context.getText(stringRes));
    if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
      spannable.setSpan(
          new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
    }
    return spannable;
  }

关于android - 通知彩色操作按钮 Android 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178293/

相关文章:

android - Android 10 上的自定义通知中没有展开按钮

android - 如何在录制时向视频添加音频 [ContinuousCaptureActivity] [Grafika]

android - 视频未在 Webview 中显示

java - Android:预定通知未显示?

android - 在应用程序图标上显示角标(Badge)(数字)的推荐方式是什么?

android - locationListener 在前台服务 30 秒后不起作用

java - 为不同类型的对象反序列化 JSON 数据

带有整数参数的Android SQLite查询

java - 为需要 : Service, 计时器、GPS、通知、 Activity 的情况寻找正确的解决方案

Android - 检测用户的操作系统主题是深色还是浅色?