字幕无法在Android Oreo上使用,我该怎么办?

时间:2018-11-28 11:40:09

标签: android notifications marquee

我正在构建一个音乐应用程序,当播放任何歌曲时,该应用程序将显示一个通知播放器,其中包括图像,艺术家姓名和标题歌曲(我为此使用了字幕效果)。大多数设备的OS 4/5/6/7都很好,除了OS 8以外,标题歌曲都可以运行字幕效果。那我该怎么办?

这是notification_player.ics.xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/notify_image_jacket"
    android:layout_alignParentRight="true"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/notify_image_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/image_description"
            android:src="@drawable/ic_notify_play" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
            <!-- #1242 - Add title color white-->
            <TextView
                android:id="@+id/notify_text_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="@color/font_color_white"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:duplicateParentState="true">
                <requestFocus android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:duplicateParentState="true"/>
            </TextView>

            <TextView
                android:id="@+id/notify_text_separator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/separator"
                android:textColor="@color/font_color_white" />

            <!-- #1242 - Add title color white-->
            <TextView
                android:id="@+id/notify_text_artist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="@color/font_color_white" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

我正在使用RemoteViews和通知频道

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        views = new RemoteViews(getPackageName(), R.layout.notification_player_ics);
    }
    views.setTextViewText(R.id.notify_text_title, LibraryUtility.getDispTrackName(mContext, title));
    views.setTextViewText(R.id.notify_text_artist, LibraryUtility.getDispArtistName(mContext, artist));

    /**#2178 - Edit - Issue for media notifications to support Android Oreo - start */
    String channelid = String.valueOf(NotificationConst.NOTIFICATION_MEDIASERVICE);

    int pFlag = PendingIntent.FLAG_UPDATE_CURRENT;
    //PLUS_MUSE-1194 【SCL23】通知エリアを押下した時にアプリが復帰しない対応
    if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
        //4.4の一部端末でFLAG_UPDATE_CURRENTが使用できないためFLAG_CANCEL_CURRENTで代用
        pFlag = PendingIntent.FLAG_CANCEL_CURRENT;
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, pFlag);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getResources().getString(R.string.app_name);
        Utility.createChannelNotifications(mNotificationManager, NotificationManager.IMPORTANCE_LOW, name, channelid);
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        if (mNotification == null) {
            mNotification = new Notification();
        }
        mNotification.contentView = views;
        mNotification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mNotification.icon = R.drawable.ic_notify_play;
        mNotification.contentIntent = pendingIntent;
    } else {
        if (mBuilder == null) {
            mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelid);
        }
        mBuilder.setSmallIcon(R.drawable.ic_notify_play);
        mBuilder.setCustomContentView(views);
        //mBuilder.setContent(views);
        mBuilder.setAutoCancel(false);
        mBuilder.setOngoing(true);
        mBuilder.setColor(ContextCompat.getColor(mContext, R.color.bg_color_black));
        mBuilder.setContentIntent(pendingIntent);
    }

这是createChannelNotifications()方法:

public static void createChannelNotifications(NotificationManager notificationManager, int importance, CharSequence name, String channelid) {
    String id = channelid;
    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    mChannel.enableLights(true);
    mChannel.enableVibration(true);
    mChannel.setLightColor(Color.GREEN);
    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    mChannel.setVibrationPattern(new long[]{0});
    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用它在Oreo中对我有用。...

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#55000000"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:freezesText="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:padding="5dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:textStyle="bold" />