我有3个测试设备。我无法在Android 6.0和4.3中显示自定义通知,而相同的代码就像5.1.1中的魅力一样
我真的厌倦了Android的兼容性困境。
这是我在Android 5.1.1中出现的自定义通知:
这是我的代码:
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
中的一些组件,但我认为没有不合适的组件。任何建议将不胜感激。
答案 0 :(得分:1)
我解决了自己的问题。
根据this answer,我删除了ImageButtons中的android:onClick
属性。然后它适用于我的所有设备。
答案 1 :(得分:0)