- 原文地址:Migrating MediaStyle notifications to support Android O
- 原文做者:Nazmul Idris (Naz)
- 译文出自:掘金翻译计划
- 本文永久连接:github.com/xitu/gold-m…
- 译者: ppp-man
- 校对者:llp0574 zhaochuanxing
若是你在 API level 25 或如下的版本上用 MediaStyle
的提醒功能,这篇文章充当把这功能迁移到 Android O 上的指引。MediaStyle
的提醒功能一般是有限制的,并在后台开启那些容许音频回放的服务。html
Android O 的一些主要的区别须要被考虑到。前端
[startForegroundService(Intent)](https://developer.android.cn/preview/features/background.html#services)
开头, 并且五秒内必定要出现个持续性的提醒。整合到 Android O 的迁移须要如下几个小步骤。java
记得把下面的代码加到你的导入语句中:react
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.app.NotificationCompat.MediaStyle;</pre>
复制代码
或许以前会有 v7 的导入语句,但如今已经再也不须要:android
import android.support.v7.app.NotificationCompat;</pre>
复制代码
如今你的 build.gradle
文件里,只须要导入包含 MediaStyle
类的 media-compat
函数库。ios
implementation ‘com.android.support:support-media-compat:26.+’</pre>
复制代码
MediaStyle
在 android.support.v4.media
这个包里由于它如今是 [media-compat](https://developer.android.cn/topic/libraries/support-library/packages.html#v4-media-compat)
依赖的一部分。特地不将它们放在 support-compat
库里的缘由是保持支持库模块里的关注点分离。git
为了在 Android O 里用到提醒功能,你必定要用提醒渠道。v4 支持库如今有为了建立提醒的新构造器:github
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, CHANNEL_ID);</pre>
复制代码
老的构造器到了 26.0.0 版的支持库就不能用了,于是你在用 API 26 的时候提醒就不会显示(由于渠道在 API 26 里是提醒功能的先要条件):后端
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext);</pre>
复制代码
为了更好地理解 Android O 里的渠道,请在 developer.android.cn 上阅读全部相关信息。Google Play Music 可让你自定义提醒消息。例如,若是你只关心”重放“相关的提醒,就能够只启用与之相关的提醒并禁用其余。bash
NotificationCompat
这个类并不帮你建立渠道,你依然要本身建立一个。这里有一个 Android O 的例子。
private static final String CHANNEL_ID = "media_playback_channel";
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationManager
mNotificationManager =
(NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
// 渠道 ID
String id = CHANNEL_ID;
// 用户看到的渠道名字
CharSequence name = "Media playback";
// 用户看到的渠道描述
String description = "Media playback controls";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// 渠道的配置
mChannel.setDescription(description);
mChannel.setShowBadge(false);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotificationManager.createNotificationChannel(mChannel);
}
复制代码
这段代码利用 NotificationCompat
生成 MediaStyle
提醒。
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.app.NotificationCompat.MediaStyle;
//...
// 你只须要在 API 26 以上的版本建立渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, CHANNEL_ID);
notificationBuilder
.setStyle(
new MediaStyle()
.setMediaSession(token)
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
mContext, PlaybackStateCompat.ACTION_STOP)))
.setColor(ContextCompat.getColor(mContext, R.color.notification_bg))
.setSmallIcon(R.drawable.ic_stat_image_audiotrack)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOnlyAlertOnce(true)
.setContentIntent(createContentIntent())
.setContentTitle(“Album”)
.setContentText(“Artist”)
.setSubText(“Song Name”)
.setLargeIcon(MusicLibrary.getAlbumBitmap(mContext, description.getMediaId()))
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
mService, PlaybackStateCompat.ACTION_STOP));
view rawMediaStyleNotification.java hosted with ❤ by GitHub
复制代码
在 Android O里,像音乐重放这类理应是在后台运行的服务须要用 Context.startForegroundService()
而不是 Context.startService()
来启动。若是你在 Android O 上,就能够用 ContextCompat
这个类来自动帮你完成,若是你在 Android N 或以前的版本就须要用 startService(Intent)
来启动。
if (isPlaying && !mStarted) {
Intent intent = new Intent(mContext, MusicService.class);
ContextCompat.startForegroundService(mContext, intent);
mContext.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
}
复制代码
就是那么简单!三个简单步骤就能帮你把 MediaStyle
的后台提醒功能从 Android O 以前的版本迁移到 Android O 上。
关于 MediaStyle
更新的更多资讯,请看这里
掘金翻译计划 是一个翻译优质互联网技术文章的社区,文章来源为 掘金 上的英文分享文章。内容覆盖 Android、iOS、React、前端、后端、产品、设计 等领域,想要查看更多优质译文请持续关注 掘金翻译计划、官方微博、知乎专栏。