Search code examples
androidandroid-layoutnotificationsandroid-custom-viewremoteview

Custom Notification Layout crashes on both Android 6.0 and 4.3


I have 3 test devices. I cannot display customized Notification in Android 6.0 and 4.3 while the same code works like a charm in 5.1.1

I really tired of Android's compatibility dilemma.

This is my customized notification appearing in Android 5.1.1:

Customized Notification

Here is my code:

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);
}

And the exception causing crash:

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)

I am also giving my notificaiton XML Layout:

<?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>

So where is the problem in my implementation? I know I have to use only some of components in my RemoteView but I think there is no inappropriate component. Any advices will be much appreciated.


Solution

  • I resolved my own problem.

    According to this answer, I removed android:onClick attributes in my ImageButtons. Then it works on all of my devices.