gpt4 book ai didi

java - Android 8.1 带通知 : Use of stream types is deprecated

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:03:16 26 4
gpt4 key购买 nike

只要我的服务正常运行,我的状态栏中就会保留 FLAG_ONGOING_EVENT 通知,并且它每秒都会更新时间。

Android 8之后,我添加了Notification Channel 8 和 8+ 设备都运行良好,但是 8+ 设备每秒都在我的 logcat 中填充以下警告,这非常烦人:

04-10 20:36:34.040 13838-13838/xxx.xxxx.xxxx W/Notification: Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

我很确定我没有为 channel 或通知本身设置任何声音

以下是我创建通知 channel 的方式:

channel = new NotificationChannel(CHANNEL_ONGOING_ID,
getString(R.string.channel_ongoing_name),
NotificationManager.IMPORTANCE_LOW);
channel.setDescription(getString(R.string.channel_ongoing_desc));
mNotificationManager.createNotificationChannel(channel);

还有我的通知:

RemoteViews view = new RemoteViews(getPackageName(),
R.layout.notification_ongoing);

view.setImageViewResource(R.id.notification_button,
mState == STATE_ONGOING ? R.drawable.ic_pause : R.drawable.ic_start);
view.setTextViewText(R.id.notification_text, Utils.getReadableTime(mMilliseconds));

view.setOnClickPendingIntent(R.id.notification_button,
PendingIntent.getBroadcast(mContext, 1, new Intent(INTENT_NOTIFICATION_TOGGLE),
PendingIntent.FLAG_UPDATE_CURRENT));

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,
CHANNEL_ONGOING_ID);

builder.setContent(view)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_app_icon)
.setContentIntent(PendingIntent.getActivity(mContext, 0,
new Intent(mContext, MainActivity.class), 0));

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// before android 8 notification takes care of priority by itself, and start
// from 8 the priority is with the channel.
builder.setPriority(Notification.PRIORITY_DEFAULT);
}

Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;

我确实尝试为 channel 设置声音,就像我真的不需要发出任何声音一样,但警告仍然遍布我的 logcat。

channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
new AudioAttributes.Builder().setUsage(
AudioAttributes.USAGE_NOTIFICATION).build())

有什么想法吗?非常感谢您的帮助!

最佳答案

与持久性 Service Notification 有完全相同的问题,并尝试了 setSound(null, null) 的多种变体,等等 NotificationChannel 并使用 NotificationCompat

解决方案是在 Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 时消除 NotificationCompat,因为支持库 (appcompat-v7:26.1.0)仍然实现已弃用的 Builder.setSound() 方法,这些方法隐含地使用了导致警告的 STREAM_ 类型。

所以我现在执行以下操作,警告消失了:

Notification mNotification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotification =
new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle("API Service")
.setContentText("Service is running")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent)
.setTicker("API")
.build();
} else {
mNotification =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle("API Service")
.setContentText("Service is running")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(null)
.setContentIntent(pendingIntent)
.setTicker("API")
.build();
}

关于java - Android 8.1 带通知 : Use of stream types is deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49754699/

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