Android 8.1 W/Notification:不推荐使用流类型

Posted

技术标签:

【中文标题】Android 8.1 W/Notification:不推荐使用流类型【英文标题】:Android 8.1 W/Notification: Use of stream types is deprecated 【发布时间】:2018-04-10 13:16:39 【问题描述】:

只要我的服务正常工作,状态栏中就会保留一个 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 = 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;

我确实尝试为频道设置声音,就像我真的不需要发出任何声音一样,但警告仍然保留在我的 logcat 中。

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

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

【问题讨论】:

我认为这可能会帮助你***.com/questions/45395669/… ***.com/questions/45919392/… 我知道的老问题,但在支持库更新之前,这是答案 【参考方案1】:

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

解决方案是在 Build.VERSION.SDK_INT &gt;= 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();

【讨论】:

但是 Notification.Builder 很久以前就被弃用了,确定吗?

以上是关于Android 8.1 W/Notification:不推荐使用流类型的主要内容,如果未能解决你的问题,请参考以下文章

揭秘最新成果:Android 8.1

Android Studio3.0配置Butterknife8.8.1及大坑

Zebra 设备-TC75x-Android 8.1-条形码扫描问题

AdapterView 不支持 Android 8.1/9.0 removeView(View)

Android startActivity原理分析(基于Android 8.1 AOSP)

Android 8.1 修改默认通知声