NotificationCompat.setStyle()不能得到Notification.MediaStylesetStyle、NotificationCompat、MediaStyle、Notif

由网友(临淵不羡鱼)分享简介:我发现原来MediaStyle的setStyle是不是当我用工作NotificationCompat.builder(本)发出通知。I've found out that the MediaStyle setStyle is not working when I'm using NotificationCompat...

我发现原来MediaStyle的setStyle是不是当我用工作 NotificationCompat.builder(本)发出通知。

I've found out that the MediaStyle setStyle is not working when I'm using NotificationCompat.builder(this) to make a notification.

我的意思是,当NotificationCompat.Builder对用这个(这个)

I mean, When using this on NotificationCompat.Builder(this):

.setStyle(new Notification.MediaStyle()
         .setMediaSession(mySession))

它说,它希望获得 NotificationCompat.style 而不是 Notification.MediaStyle

你能不能帮我解决这个问题? 是否有任何替代NotificationCompat?

Can you help me solve that problem? Is there any replacement for the NotificationCompat?

感谢。

推荐答案

这个问题已经得到解决,如果你在的Andr​​oid链接支持库版本22.2.1 (2015年七月):

Update

This problem is fixed now if you link in Android Support Library revision 22.2.1 (July 2015):

提供的 NotificationCompat.MediaStyle

下面是我的解决方法的有一个在支持库不NotificationCompat.MediaStyle。

Here's my workaround for "There is no NotificationCompat.MediaStyle in the support library".

它避免了重复应用逻辑和可以很容易地背出解决办法,如果/当NotificationCompat支持 MediaStyle

It avoids duplicating application logic and makes it easy to back out the workaround if/when NotificationCompat supports MediaStyle.

目的:这可以很容易地使用 MediaStyle 的API 21+(提供紧凑型和扩展通知布局,带有按钮),或在旧版本的Andr​​oid(只是一个布局,按钮)。

Purpose: This makes it easy to use the MediaStyle in API 21+ (offering compact and expanded notification layouts, with buttons), or an ordinary notification layout on older versions of Android (just one layout, with buttons).

注意::如果你的目标是把多个版本的Andr​​oid锁屏界面上的按钮,就可以用这个方法来实现锁屏通知API 21+,同时也实现锁屏窗口小部件对老年人的API。

Note: If your goal is to put buttons on the lock screen on multiple versions of Android, you can use this approach to implement lock screen notifications for API 21+, and also implement a lock screen widget for older APIs.

如何:首先,创建一个新的通知建设者兼容包,从一个接口交替底层实现。从 NotificationCompat.Builder 婴儿床,剥离下来到所需要的功能:

How to: First, create a new notification builder compatibility package, starting with an interface to alternate underlying implementations. Crib from NotificationCompat.Builder, stripped down to the needed features:

public interface NotificationBuilder {
    public NotificationBuilder setWhen(long when);
    public NotificationBuilder setUsesChronometer(boolean b);
    public NotificationBuilder setSmallIcon(int icon);
    // ...

    /** Sets MediaStyle with setShowActionsInCompactView(). */
    public NotificationBuilder setMediaStyleActionsInCompactView(int... actions);

    public Notification build();
}

二,使建立在一个实施 NotificationCompat.Builder

public class V20Builder implements NotificationBuilder {
    private NotificationCompat.Builder builder;

    public V20Builder(Context context) {
        builder = new NotificationCompat.Builder(context);
    }

    @Override
    public NotificationBuilder setWhen(long when) {
        builder.setWhen(when);
        return this;
    }

    @Override
    public NotificationBuilder setUsesChronometer(boolean b) {
        builder.setUsesChronometer(b);
        return this;
    }

    @Override
    public NotificationBuilder setSmallIcon(int icon) {
        builder.setSmallIcon(icon);
        return this;
    }

    // ...

    @Override
    public NotificationBuilder setMediaStyleActionsInCompactView(int... actions) {
        // Noop for Android API V20-.
        return this;
    }

    @Override
    public Notification build() {
        return builder.build();
    }
}

三是建立在一个实施 Notification.Builder

@TargetApi(21)
public class V21Builder implements NotificationBuilder {
    private Notification.Builder builder;

    public V21Builder(Context context) {
        builder = new Notification.Builder(context);
    }

    @Override
    public NotificationBuilder setWhen(long when) {
        builder.setWhen(when);
        return this;
    }

    @Override
    public NotificationBuilder setUsesChronometer(boolean b) {
        builder.setUsesChronometer(b);
        return this;
    }

    @Override
    public NotificationBuilder setSmallIcon(int icon) {
        builder.setSmallIcon(icon);
        return this;
    }

    // ...

    @Override
    public NotificationBuilder setMediaStyleActionsInCompactView(int... actions) {
        new Notification.MediaStyle(builder).setShowActionsInCompactView(actions);
        return this;
    }

    @Override
    public Notification build() {
        return builder.build();
    }
}

四,添加一个工厂方法实例正确的建设者:

Fourth, add a factory method to instantiate the right builder:

public NotificationBuilder makeBuilder() {
  if (Build.VERSION.SDK_INT >= 21) { // Load the API V21 class only if the OS can load it.
      return new V21Builder(context);
  }
  return new V20Builder(context);
}
阅读全文

相关推荐

最新文章