我有一个正在进行的通知,其中的水平字幕定义为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="52dp"
android:layout_height="52dp"
android:background="@mipmap/ic_launcher_round"
android:contentDescription="@string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="false"
android:singleLine="true" >
<requestFocus />
</TextView>
</LinearLayout>
</LinearLayout>
和
fun createNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "channel_name"
val descriptionText = "channel_description"
val importance = Notificationmanager.IMPORTANCE_HIGH
val channel = NotificationChannel("CHANNEL_ID",name,importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationmanager: Notificationmanager =
context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as Notificationmanager
notificationmanager.createNotificationChannel(channel)
}
}
suspend fun showNotification(context: Context) {
var collapsedView = RemoteViews(context.packageName,R.layout.default_notification_collapsed)
collapsedView.setTextViewText(
R.id.text,"Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
val builder = NotificationCompat.Builder(context.applicationContext,"CHANNEL_ID")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(collapsedView)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setOngoing(true)
with(NotificationmanagerCompat.from(context.applicationContext)) {
notify(1,builder.build())
}
}
这在Oreo上正常工作,但是当其他应用发出通知时,字幕将在锁定屏幕上停止。这似乎发生在来自Gmail和Twitter的通知中,但没有出现在系统通知中,例如“检测到打开的wifi”。
此外,它似乎不会立即发生。收到新通知时,它会出现在我正在进行的通知上方(选取框继续滚动),几秒钟后,android会重新排列这些通知,以便我进行中的通知显示在顶部,选取框停止。
这是预期的行为吗?如何防止这种情况发生?