与MediaSession Android Wear通知 [英] Android Wear Notification with MediaSession

查看:529
本文介绍了与MediaSession Android Wear通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出使用有一个页面上的几个按钮和一个不同的页面上的其他一些按钮MediaSession一个Android磨损通知。它看起来像谷歌立即游戏在Android Wear应用的通知。我跟着这个教程GitHub的<一个href=\"https://github.com/PaulTR/AndroidDemoProjects/blob/master/MediaSessionwithMediaStyleNotification/app/src/main/java/com/ptrprograms/mediasessionwithmediastylenotification/MediaPlayerService.java\" rel=\"nofollow\">https://github.com/PaulTR/AndroidDemoProjects/blob/master/MediaSessionwithMediaStyleNotification/app/src/main/java/com/ptrprograms/mediasessionwithmediastylenotification/MediaPlayerService.java

然而,每个动作被添加到一个单独的页面上android的磨损。我希望将他们中的一些成一页。例如,播放/暂停,pre和下一个页和第二页上的速度按钮。我想知道是否有可能实现这一具有自定义通知,而不为了使用MediaSession涵盖API小于21

谢谢!

 私人无效buildNotification(Notification.Action动作){
            Notification.MediaStyle风格=新Notification.MediaStyle();            意向意图=新意图(getApplicationContext(),MediaPlayerService.class);
            intent.setAction(ACTION_STOP);
            的PendingIntent的PendingIntent = PendingIntent.getService(getApplicationContext(),1,意向,0);
            Notification.Builder建设者=新Notification.Builder(本)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(媒体标题)
                    .setContentText(媒体艺术家)
                    .setDeleteIntent(的PendingIntent)
                    .setStyle(样式);            builder.addAction(generateAction(android.R.drawable.ic_media_ previous,previous,ACTION_ preVIOUS));
            builder.addAction(generateAction(android.R.drawable.ic_media_rew,倒带,ACTION_REWIND));
            builder.addAction(动作);
            builder.addAction(generateAction(android.R.drawable.ic_media_ff,快速向前的,ACTION_FAST_FORWARD));
            builder.addAction(generateAction(android.R.drawable.ic_media_next,下一步,ACTION_NEXT));
            style.setShowActionsInCompactView(0,1,2,3,4);            NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1,builder.build());
    }

更新时间是code:在类型NotificationCompat.MediaStyle的方法setMediaSession(MediaSessionCompat.Token)不适用于参数(MediaSession.Token):当我通过它令牌的setMediaSession是给这个编译错误。 3行动仍在显示在Android磨损3单独的页面。

 私人无效buildNotification(Notification.Action动作){
    NotificationCompat.MediaStyle风格=新NotificationCompat.MediaStyle();
    //style.setMediaSession(mSession.getSessionToken());
    style.setMediaSession(NULL);
    style.setShowActionsInCompactView(1,2);    位图图标= BitmapFactory.de codeResource(this.getResources(),R.drawable.pinkfloyd);    NotificationCompat.Builder建设者=新NotificationCompat.Builder(本);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setLargeIcon(图标);
    builder.setContentTitle(媒体标题);
    builder.setContentText(媒体艺术家);
    builder.setColor(Color.argb(0,60,13,77));
    builder.setStyle(样式);    builder.addAction(R.drawable.ic_launcher,
            测试1,NULL);
    builder.addAction(R.drawable.ic_launcher,
            Test2的,NULL);
    builder.addAction(R.drawable.ic_launcher,
            Test3的,NULL);
    NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1,builder.build());
}


解决方案

在应用程序兼容性v22.2.0添加一个特性是的 NotificationCompat.MediaStyle - 这可让您建立媒体的通知,并通过媒体信息Android 5.0以上版本的设备向后兼容的方​​式,一起工作的<一个href=\"https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.html\"相对=nofollow> MediaSessionCompat 提供信息到Android穿旧设备上(以及对API14-19设备将屏幕锁定控制)。

当你构建一个<一个href=\"https://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html\"相对=nofollow> MediaStyle 通知,关键是要叫<一个href=\"https://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html#setMediaSession(android.support.v4.media.session.MediaSessionCompat.Token)\"相对=nofollow> setMediaSession()告诉Android的穿您的通知连接到媒体播放功能,让您在单卡样式的通知4的动作。

I would like to make an android wear notification using MediaSession that has several buttons on one page and some other buttons on a different page. It would look like Google Play Now App notification on an android wear. I followed this github tutorial at https://github.com/PaulTR/AndroidDemoProjects/blob/master/MediaSessionwithMediaStyleNotification/app/src/main/java/com/ptrprograms/mediasessionwithmediastylenotification/MediaPlayerService.java

However, each action was added to a separate page on the android wear. I want to group some of them into one page. For instance, the play/pause, pre and next on one page and the rate button on a second page. I would like to know if it is possible to achieve that with a custom notification without using MediaSession in order to cover API less than 21.

Thanks!

private void buildNotification( Notification.Action action ) {
            Notification.MediaStyle style = new Notification.MediaStyle();

            Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
            intent.setAction( ACTION_STOP );
            PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
            Notification.Builder builder = new Notification.Builder( this )
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle( "Media Title" )
                    .setContentText( "Media Artist" )
                    .setDeleteIntent( pendingIntent )
                    .setStyle(style);

            builder.addAction( generateAction( android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS ) );
            builder.addAction( generateAction( android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND ) );
            builder.addAction( action );
            builder.addAction( generateAction( android.R.drawable.ic_media_ff, "Fast Foward", ACTION_FAST_FORWARD ) );
            builder.addAction( generateAction( android.R.drawable.ic_media_next, "Next", ACTION_NEXT ) );
            style.setShowActionsInCompactView(0,1,2,3,4);

            NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
            notificationManager.notify( 1, builder.build() );
    }

UPDATED CODE: The the setMediaSession is giving this compiling error when I pass it a token: The method setMediaSession(MediaSessionCompat.Token) in the type NotificationCompat.MediaStyle is not applicable for the arguments (MediaSession.Token). The 3 actions are still being displayed on 3 separate pages on the android wear.

private void buildNotification( Notification.Action action ) {
    NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
    //style.setMediaSession(mSession.getSessionToken());            
    style.setMediaSession(null);            
    style.setShowActionsInCompactView(1,2);

    Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.pinkfloyd);

    NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setLargeIcon(icon);
    builder.setContentTitle( "Media Title" );
    builder.setContentText( "Media Artist" );
    builder.setColor(Color.argb(0, 60, 13, 77));
    builder.setStyle(style);

    builder.addAction(R.drawable.ic_launcher,
            "Test1 ", null);
    builder.addAction(R.drawable.ic_launcher,
            "Test2 ", null);
    builder.addAction(R.drawable.ic_launcher,
            "Test3 ", null);
    NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
    notificationManager.notify( 1, builder.build() );
}

解决方案

One feature added in AppCompat v22.2.0 is NotificationCompat.MediaStyle - this gives you a backward compatible way to build media notifications and pass media information to Android 5.0+ devices, working alongside MediaSessionCompat to provide information to Android Wear on older devices (as well as adding lockscreen controls on API14-19 devices).

When you build a MediaStyle notification, it is critical to call setMediaSession() which tells Android Wear that your notification is attached to media playback, giving you the 4 action on a single card style notification.

这篇关于与MediaSession Android Wear通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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