gpt4 book ai didi

android - 自定义通知布局在 Android 6.0 和 4.3 上崩溃

转载 作者:行者123 更新时间:2023-11-30 01:16:15 26 4
gpt4 key购买 nike

我有 3 个测试设备。我无法在 Android 6.0 和 4.3 中显示自定义通知,而相同的代码在 5.1.1 中就像一个魅力

我真的厌倦了 Android 的兼容性困境。

这是我在 Android 5.1.1 中出现的自定义通知:

Customized Notification

这是我的代码:

private void notification(Radio radio, int playPauseImage) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_notification);
contentView.setImageViewResource(R.id.imgChannelLogo, Utils.getChannelLogoByPosition(currentRadioId));
contentView.setTextViewText(R.id.txtChannelName, radio.getKanalAdi());
contentView.setTextViewText(R.id.txtCategory, radio.getKategoriAdi());
contentView.setImageViewResource(R.id.mediacontroller_previous, android.R.drawable.ic_media_rew);
contentView.setImageViewResource(R.id.mediacontroller_play_pause, playPauseImage);
contentView.setImageViewResource(R.id.mediacontroller_next, android.R.drawable.ic_media_ff);

PendingIntent pendingPlayPauseIntent = PendingIntent.getBroadcast(this, 0, new Intent(PLAYPAUSE), 0);
PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this, 0, new Intent(NEXT), 0);
PendingIntent pendingPreviousIntent = PendingIntent.getBroadcast(this, 0, new Intent(PREVIOUS), 0);
PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, new Intent(CLOSE), 0);

contentView.setOnClickPendingIntent(R.id.mediacontroller_play_pause, pendingPlayPauseIntent);
contentView.setOnClickPendingIntent(R.id.mediacontroller_next, pendingNextIntent);
contentView.setOnClickPendingIntent(R.id.mediacontroller_previous, pendingPreviousIntent);
contentView.setOnClickPendingIntent(R.id.imgClose, pendingCloseIntent);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_status)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContent(contentView)
.setWhen(System.currentTimeMillis())
.setTicker(getText(R.string.ticker));

notificationBuilder.mNotification.bigContentView = contentView;

startForeground(1337, notificationBuilder.mNotification);
}

导致崩溃的异常:

FATAL EXCEPTION: main
Process: com.mypackage.myapp, PID: 12134
android.app.RemoteServiceException: Bad notification posted from package com.mypackage.myapp: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.mypackage.myapp user=UserHandle{0} id=1337 tag=null score=0 key=0|com.mypackage.myapp|1337|null|10166: Notification(pri=0 contentView=com.mypackage.myapp/0x7f04001a vibrate=null sound=null tick defaults=0x0 flags=0x62 color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我还给出了我的通知 XML 布局:

<?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="176dp"
android:background="#181818"
android:orientation="vertical" >

<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">

<ImageView
android:id="@+id/imgChannelLogo"
android:layout_width="96dp"
android:layout_height="96dp" />

<LinearLayout
android:id="@+id/llRadioInfo"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/imgChannelLogo"
android:layout_toRightOf="@+id/imgChannelLogo"
android:layout_toLeftOf="@+id/imgClose"
android:layout_toStartOf="@+id/imgClose"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="0dp"
android:paddingEnd="0dp">

<TextView
android:id="@+id/txtChannelName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@android:color/white"
android:textStyle="bold"/>

<TextView
android:id="@+id/txtCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"/>
</LinearLayout>

<ImageView
android:id="@+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@android:drawable/ic_menu_close_clear_cancel"
android:layout_gravity="center" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/mediacontroller_bg">

<ImageButton
android:id="@+id/mediacontroller_previous"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/mediacontroller_play_pause"
android:layout_toStartOf="@+id/mediacontroller_play_pause"
android:background="@drawable/mediacontroller_button"
android:contentDescription="@string/mediacontroller_previous"
android:src="@android:drawable/ic_media_rew"
android:onClick="previousChannel"
android:scaleType="fitCenter" />

<ImageButton
android:id="@+id/mediacontroller_play_pause"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/mediacontroller_button"
android:contentDescription="@string/mediacontroller_play_pause"
android:src="@drawable/mediacontroller_pause"
android:scaleType="fitCenter"
android:paddingTop="10dp"
android:paddingBottom="10dp" />

<ImageButton
android:id="@+id/mediacontroller_next"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/mediacontroller_play_pause"
android:layout_toEndOf="@+id/mediacontroller_play_pause"
android:background="@drawable/mediacontroller_button"
android:contentDescription="@string/mediacontroller_next"
android:src="@android:drawable/ic_media_ff"
android:onClick="nextChannel"
android:scaleType="fitCenter" />

</RelativeLayout>
</LinearLayout>

那么我的实现哪里出了问题呢?我知道我必须在我的 RemoteView 中只使用一些组件,但我认为没有不合适的组件。任何建议将不胜感激。

最佳答案

我解决了我自己的问题。

根据 this answer ,我删除了 ImageButtons 中的 android:onClick 属性。然后它可以在我的所有设备上运行。

关于android - 自定义通知布局在 Android 6.0 和 4.3 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com