MediaStyle通知以在紧凑视图中显示按钮 [英] MediaStyle Notification to Show Button in Compact View

查看:58
本文介绍了MediaStyle通知以在紧凑视图中显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MediaStyle用于推送通知,以便可以显示当前正在播放的歌曲元数据以及包括暂停按钮.我看到的问题是,即使我包含了setShowActionsInCompactView(),通知也不会自动扩展以显示暂停按钮.如果在锁定屏幕上按下通知时,通知会展开,然后显示暂停按钮.但是我希望它不扩展显示按钮.

I'm using the MediaStyle for my push notification so that I can show currently playing song meta data as well as include a pause button. The issue I'm seeing is that my notification is not automatically expanding to show the pause button, even though I've included the setShowActionsInCompactView(). If I pull down on my notification while on the lock screen, it expands and then shows the pause button. But I would like it to show the button without expanding.

如何显示通知的屏幕截图: http://cl.ly/image/3E2D0m403v1b

Screenshot of how notification is displayed: http://cl.ly/image/3E2D0m403v1b

在下拉通知扩展后如何显示通知的屏幕截图: http://cl.ly/image/1N1i0G121i2Y

Screenshot of how notification is displayed after pulling down on it to expand: http://cl.ly/image/1N1i0G121i2Y

以下是我用来生成通知的代码段:

Below is the code snippet I'm using to generate the notification:

        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Intent intent = new Intent(BROADCAST_PLAYER_STOP);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setTicker(tickerString)
                .setContentText(contentString)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(icon)
                .addAction(R.drawable.ic_media_pause, "", pendingIntent)
                .setContentIntent(pi)
                .setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
                        .setShowActionsInCompactView(0)
                        .setMediaSession(mSession.getSessionToken()))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setWhen(0)
                .setOngoing(true);
        startForeground(NOTIFICATION_ID, notification.build());

推荐答案

要使用 NotificationCompat.MediaStyle ,必须使用 android.support.v7.app.NotificationCompat.Builder -默认的v4 Builder无法处理v7 MediaStyle

To use NotificationCompat.MediaStyle, you must use the android.support.v7.app.NotificationCompat.Builder - the default v4 Builder does not handle the v7 MediaStyle

这篇关于MediaStyle通知以在紧凑视图中显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆