我想为我的音乐播放器创建一个通知。我想使用两种不同的自定义通知,一种用于通知区域,另一种用于锁屏。我在通知生成器中将setPublicVersion用于锁定屏幕,但无法正常工作。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "media");
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setOnClickPendingIntent(R.id.play, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
PlaybackStateCompat.ACTION_PLAY_PAUSE));
remoteViews.setOnClickPendingIntent(R.id.pause, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
PlaybackStateCompat.ACTION_PLAY_PAUSE));
builder
.setCustomContentView(remoteViews)
.setContentIntent(contentPendingIntent)
.setSmallIcon(R.drawable.logo)
.setVisibility(VISIBILITY_PRIVATE)
.setPublicVersion(getLockScreenBuilder(state))
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), PlaybackStateCompat.ACTION_STOP));
startForeground(10, builder.build());
//这是锁定屏幕的通知
private Notification getLockScreenBuilder(PlaybackStateCompat state) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "media");
PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, getPlayerIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_lock_screen);
remoteViews.setImageViewBitmap(R.id.track_name, textAsBitmap(currentTrack.getTitle(), Typeface.createFromAsset(getAssets(), "iran_sans_mobile_fa_num_bold.ttf"), 45));
remoteViews.setImageViewBitmap(R.id.channel_title, textAsBitmap(channelTitle, Typeface.createFromAsset(getAssets(), "iran_sans_mobile_fa_num_medium.ttf"), 40));
if (state.getState() == PlaybackStateCompat.STATE_PLAYING) {
remoteViews.setViewVisibility(R.id.pause, VISIBLE);
remoteViews.setViewVisibility(R.id.play, GONE);
} else {
remoteViews.setViewVisibility(R.id.pause, GONE);
remoteViews.setViewVisibility(R.id.play, VISIBLE);
}
remoteViews.setOnClickPendingIntent(R.id.play, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
PlaybackStateCompat.ACTION_PLAY_PAUSE));
remoteViews.setOnClickPendingIntent(R.id.pause, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
PlaybackStateCompat.ACTION_PLAY_PAUSE));
remoteViews.setOnClickPendingIntent(R.id.previous, MediaButtonReceiver.buildMediaButtonPendingIntent
(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS));
remoteViews.setOnClickPendingIntent(R.id.next, MediaButtonReceiver.buildMediaButtonPendingIntent
(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT));
Glide.with(getApplicationContext()).load(Url.ROOT_URL + "storage/podcasts/" + currentTrack.getCoverImage()).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget<Bitmap>(100, 100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
remoteViews.setImageViewBitmap(R.id.image, resource);
}
});
builder.setCustomContentView(remoteViews)
.setContentIntent(contentPendingIntent)
.setSmallIcon(R.drawable.logo)
.setVisibility(VISIBILITY_PUBLIC)
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), PlaybackStateCompat.ACTION_STOP));
return builder.build();
}
我的问题是锁定屏幕通知与用于通知区域的自定义视图相同,锁定屏幕通知的自定义视图不起作用
答案 0 :(得分:0)
AFAIK,仅在锁定屏幕上显示 的另一种通知布局是一项不存在的功能!
但是,可能会有一些想法或解决方法来实现此目的,例如监听或观察屏幕开/关事件以更新/替换您的通知。
您可能可以通过设置可以检测这三个动作的广播接收器来实现此目的:
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
并添加代码应使用KeyguardManager
检查屏幕是否锁定,here是一个很好的例子。