gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:27:41 25 4
gpt4 key购买 nike

我正在尝试像这样在通知中为按钮(操作)着色。
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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com