7

Ich verwende Glide für alle Bilder vom Server geladen, aber ich habe die Fehlersuche versucht, sie auf korrekte Weise zu Benachrichtigungen und RemoteControlClientCompat (das coole Ding mit Sperrbildschirmen) zu setzen. Ich entwickle einen Musik-Player, so dass sich jedes Mal, wenn ein Song geändert wird, das Cover von Benachrichtigungen ändern muss. Ich habe diesen Code und es funktioniert zum ersten Mal (obwohl das Bild von URL geladen wird oder aus Zeichen), aber nicht, wenn es zum zweiten Mal aufgerufen wird. Das Bild ändert sich nicht! CustomNotification wird aufgerufen, wenn ein Song geändert wird. Und RegisterRemoteClient wird bei der Startaktivität aufgerufen.Wie funktioniert Glide in remoteViews?

Wenn dies nicht der richtige Weg ist, sagen Sie bitte wie.

public void CustomNotification() { 
    Log.d(TAG, "Set custom notification"); 

    simpleRemoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_custom); 
    expandedRemoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_big); 

    mNotification = new NotificationCompat.Builder(getApplicationContext()) 
      .setSmallIcon(R.mipmap.ic_main_logo) 
      .setTicker(mSongTitle + " - " + mSongAuthors) 
      .setContentTitle(mSongTitle).build(); 

    setRemoteListeners(simpleRemoteView); 
    setRemoteListeners(expandedRemoteView); 

    try { 
     //Check if playingSong has a cover url, if not set one from drawable 
     if(!playingSong.has("cover")){ 
      mDummyAlbumArt = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album); 
      mDummyAlbumArt2 = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album); 
      mDummyAlbumArt3 = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album); 
     }else { 
      Glide.with(MainActivity.context) 
        .load(API.imgUrl(playingSong.getString("cover_img"))) 
        .asBitmap() 
        .into(new SimpleTarget<Bitmap>(100, 100) { 
         @Override 
         public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { 
          mDummyAlbumArt = bitmap; 
          mDummyAlbumArt2 = bitmap; 
          mDummyAlbumArt3 = bitmap; 
         } 
        }); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    mNotification.contentView = simpleRemoteView; 
    mNotification.contentView.setTextViewText(R.id.textSongName, mSongTitle); 
    mNotification.contentView.setTextViewText(R.id.textAlbumName, mSongAuthors); 
    mNotification.contentView.setImageViewBitmap(R.id.imageViewAlbumArt, mDummyAlbumArt); 

    if (currentVersionSupportBigNotification) { 
     mNotification.bigContentView = expandedRemoteView; 
     mNotification.bigContentView.setTextViewText(R.id.textSongName, mSongTitle); 
     mNotification.bigContentView.setTextViewText(R.id.textAlbumName, mSongAuthors); 
     mNotification.bigContentView.setImageViewBitmap(R.id.imageViewAlbumArt, mDummyAlbumArt2); 
    } 

    if (mPlayer != null) { 
     if (!mPlayer.isPlaying()) { 
      mNotification.contentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.play); 
      if (currentVersionSupportBigNotification) { 
       mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.play); 
      } 
     } else { 
      mNotification.contentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.pause); 
      if (currentVersionSupportBigNotification) { 
       mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.pause); 
      } 
     } 
    } 

    mNotification.flags |= Notification.FLAG_ONGOING_EVENT; 
    if(currentVersionSupportBigNotification) { //As priority_max only suported on API 16, the same as big notification 
     mNotification.priority = Notification.PRIORITY_MAX; 
    } 
    startForeground(NOTIFICATION_ID, mNotification); 

    //update remote controls 
    if (currentVersionSupportLockScreenControls) { 
     remoteControlClientCompat.editMetadata(true) 
       .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mSongTitle + " - " + mSongAuthors) 
       .putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, mDummyAlbumArt3) 
       .apply(); 
    } 
} 

public void setRemoteListeners(RemoteViews remoteViews){ 
    Log.d(TAG,"Setting remote listeners"); 
    PendingIntent piAppActivity = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    PendingIntent piPlayPause = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_TOGGLE_PLAYBACK), PendingIntent.FLAG_UPDATE_CURRENT); 
    PendingIntent piNext = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_SKIP), PendingIntent.FLAG_UPDATE_CURRENT); 
    PendingIntent piClose = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT); 
    PendingIntent piPrevious = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT); 

    remoteViews.setOnClickPendingIntent(R.id.linearLayoutNotification, piAppActivity); 
    remoteViews.setOnClickPendingIntent(R.id.btnPlayPause, piPlayPause); 
    remoteViews.setOnClickPendingIntent(R.id.btnNext, piNext); 
    remoteViews.setOnClickPendingIntent(R.id.btnDelete, piClose); 
    remoteViews.setOnClickPendingIntent(R.id.btnPrevious, piPrevious); 
} 

private void RegisterRemoteClient(){ 
    // Use the media button APIs (if available) to register ourselves for media button 
    // events 

    MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent); 
    // Use the remote control APIs (if available) to set the playback state 
    if (remoteControlClientCompat == null) { 
     Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     intent.setComponent(mMediaButtonReceiverComponent); 
     remoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/,0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/)); 
     RemoteControlHelper.registerRemoteControlClient(mAudioManager,remoteControlClientCompat); 
    } 

    remoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); 
    remoteControlClientCompat.setTransportControlFlags(
      RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | 
        RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | 
        RemoteControlClient.FLAG_KEY_MEDIA_NEXT | 
        RemoteControlClient.FLAG_KEY_MEDIA_STOP); 
} 

Antwort

9

Sie benötigen Verwendung NotificationTarget Klasse setzen Ihre Mitteilung Bild als glide Ziel

NotificationTarget notificationTarget = new NotificationTarget( 
    context, 
    remoteView, 
    R.id.iv_album_art, 
    notification, 
    NOTIFICATION_ID); 

zu setzen und dann dieses Ziel in üblichen gleiten Art und Weise verwenden

Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri, 
     mediaitem.getAlbumId()); 

    Glide.with(getApplicationContext()) 
    .load(uri) 
    .asBitmap() 
    .into(notificationTarget); 

Es erklärt in Glides Richtlinie hier

https://futurestud.io/blog/glide-loading-images-into-notifications-and-appwidgets

Sie könnten auch Albumcover wie änderungsbezo- Sein hier beschrieben animieren: -

https://futurestud.io/blog/glide-custom-animations-with-animate

-3

für mich Dies funktionierte:

Bitmap bitmap = Glide.with(mContext) 
       .load(YOUR_IMG_URL) 
       .asBitmap() 
       .into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL) 
       .get(); 
     remoteViews.setImageViewBitmap(R.id.widget_list_item_img,bitmap); 
+1

Diese den Faden blockieren –