0

Ich habe 3 Testgeräte. Ich kann nicht angepasste Mitteilung in Android 6.0 und 4.3 angezeigt werden, während der gleiche Code funktioniert wie ein Charme in 5.1.1Benutzerdefinierte Benachrichtigung Layout stürzt sowohl auf Android 6.0 und 4.3

ich von Android-Kompatibilität Dilemma wirklich müde.

Dies ist meine kundenspezifische Benachrichtigung im Android erscheinen 5.1.1:

Customized Notification

Hier ist mein 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); 
} 

Und die Ausnahme verursacht Absturz:

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) 

Ich gebe auch meine Benachrichtigung 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> 

Wo ist das Problem in meiner Implementierung? Ich weiß, ich muss nur einige Komponenten in meinem RemoteView verwenden, aber ich denke, es gibt keine unangemessene Komponente. Alle Ratschläge werden sehr geschätzt.

Antwort

0

Ich löste mein eigenes Problem.

Gemäß this answer habe ich Attribute in meinen ImageButtons entfernt. Dann funktioniert es auf allen meinen Geräten.

0

Die erforderlichen Felder für eine Benachrichtigung sind

  • kleines Symbol
  • Inhalt Titel
  • Inhalt Text

Sie verpassen Inhalt Titel und Inhalt Text.

Natürlich, wenn Sie ein Medium Benachrichtigung Gebäude sind, sollen Sie stattdessen NotificationCompat.MediaStyle verwenden Sie stattdessen eine benutzerdefinierte Benachrichtigung des Gebäudes.

+0

Das funktioniert nicht. Ich habe mein eigenes Problem gelöst und werde es beantworten. Inhalt Titel und Inhalt Text sind auch nicht nach meinen Tests erforderlich. –

+0

Ich habe auch diesen Mediastyle, der Bug ist immer noch da – PrisonMike