Zho*_*ouX 9 java notifications android
只要我的服务正常工作,我就会在状态栏中保留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
Run Code Online (Sandbox Code Playgroud)
我很确定我没有声音通道或通知本身.
以下是我创建通知渠道的方法:
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);
Run Code Online (Sandbox Code Playgroud)
我的通知:
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;
Run Code Online (Sandbox Code Playgroud)
我确实尝试将声音设置为频道,即使我真的不需要发出任何声音,但是警告仍然是我的整个日志.
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
new AudioAttributes.Builder().setUsage(
AudioAttributes.USAGE_NOTIFICATION).build())
Run Code Online (Sandbox Code Playgroud)
任何的想法?帮助将非常感谢!
与持久性有完全相同的问题,并尝试了与和使用Service Notification的多种变体等。setSound(null, null)NotificationChannelNotificationCompat
解决方案是消除NotificationCompatwhen Build.VERSION.SDK_INT >= Build.VERSION_CODES.O,因为支持库 (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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2851 次 |
| 最近记录: |