Android 如何在RemoteView中使用Glide?

Android 如何在RemoteView中使用Glide?,android,notifications,android-glide,remoteview,lockscreenwidget,Android,Notifications,Android Glide,Remoteview,Lockscreenwidget,我正在使用Glide从服务器加载所有图像,但我正在进行故障排除,试图以正确的方式将它们设置为notifications和RemoteControlClientCompat(带锁屏的酷东西)。我正在开发一个音乐播放器,所以每次更改歌曲时,通知中的歌曲封面都必须更改。我有这段代码,它第一次工作(虽然图像是从url或drawable加载的),但第二次调用时就不行了。图像没有改变!更改歌曲时会调用CustomNotification。并在启动活动时调用RegisterRemoteClient 如果这不是

我正在使用Glide从服务器加载所有图像,但我正在进行故障排除,试图以正确的方式将它们设置为notifications和RemoteControlClientCompat(带锁屏的酷东西)。我正在开发一个音乐播放器,所以每次更改歌曲时,通知中的歌曲封面都必须更改。我有这段代码,它第一次工作(虽然图像是从url或drawable加载的),但第二次调用时就不行了。图像没有改变!更改歌曲时会调用CustomNotification。并在启动活动时调用RegisterRemoteClient

如果这不是正确的方法,请告诉我怎么做

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);
}
public void CustomNotification(){
Log.d(标记“设置自定义通知”);
simpleRemoteView=新的远程视图(getApplicationContext().getPackageName(),R.layout.notification\u custom);
expandedRemoteView=新的远程视图(getApplicationContext().getPackageName(),R.layout.notification\u big);
mNotification=new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_主标志)
.setTicker(mSongTitle+“-”+mSongAuthors)
.setContentTitle(mSongTitle).build();
setRemoteListeners(simpleRemoteView);
setRemoteListeners(expandedRemoteView);
试一试{
//检查playingSong是否有封面url,如果没有从drawable设置封面url
如果(!playingSong.has(“封面”)){
mDummyAlbumArt=BitmapFactory.decodeResource(getResources(),R.drawable.image\u song\u相册);
mDummyAlbumArt2=BitmapFactory.decodeResource(getResources(),R.drawable.image\u song\u相册);
mDummyAlbumArt3=BitmapFactory.decodeResource(getResources(),R.drawable.image\u song\u相册);
}否则{
Glide.with(MainActivity.context)
.load(API.imgUrl(playingSong.getString(“cover\u img”))
.asBitmap()
.into(新的SimpleTarget(100100){
@凌驾
public void onResourceReady(位图、动画){
mDummyAlbumArt=位图;
mDummyAlbumArt2=位图;
mDummyAlbumArt3=位图;
}
});
}
}捕获(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);
如果(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){
如果(!mPlayer.isPlaying()){
mNotification.contentView.setImageViewResource(R.id.btnPlayPause,R.mipmap.play);
如果(currentVersionSupportBigNotification){
mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause,R.mipmap.play);
}
}否则{
mNotification.contentView.setImageViewResource(R.id.btnPlayPause,R.mipmap.pause);
如果(currentVersionSupportBigNotification){
mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause,R.mipmap.pause);
}
}
}
mNotification.flags |=Notification.FLAG_持续事件;
如果(currentVersionSupportBigNotification){//作为优先级_max仅在API 16上受支持,则与大通知相同
mNotification.priority=Notification.priority\u MAX;
}
启动前(通知ID、通知);
//更新遥控器
if(当前版本支持锁定屏幕控件){
remoteControlClientCompat.editMetadata(true)
.putString(MediaMetadataRetriever.METADATA\u KEY\u TITLE,mSongTitle+“-”+mSongAuthors)
.putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA\u KEY\u ARTWORK,mDummyAlbumArt3)
.apply();
}
}
public void setRemoteListeners(RemoteView RemoteView){
Log.d(标记“设置远程侦听器”);
PendingEvent piAppActivity=PendingEvent.getActivity(this,0,新意图(this,MainActivity.class)),
PendingEvent.FLAG_UPDATE_CURRENT);
PendingEvent piPlayPause=PendingEvent.getService(此,0,新意图(MusicService.ACTION\u TOGGLE\u PLAYBACK),PendingEvent.FLAG\u UPDATE\u CURRENT);
PendingEvent piNext=PendingEvent.getService(此,0,新意图(MusicService.ACTION\u SKIP),PendingEvent.FLAG\u UPDATE\u CURRENT);
PendingEvent piClose=PendingEvent.getService(此,0,新意图(MusicService.ACTION\u STOP),PendingEvent.FLAG\u UPDATE\u CURRENT);
PendingEvent piPrevious=PendingEvent.getService(此,0,新意图(MusicService.ACTION_先前),PendingEvent.FLAG_UPDATE_当前);
remoteViews.SetOnClickPendingContent(R.id.linearLayoutNotification,piAppActivity);
remoteViews.setOnClickPendingContent(R.id.btnPlayPause,piPlayPause);
RemoteView.setOnClickPendingContent(R.id.btnNext,piNext);
RemoteView.setOnClickPendingContent(R.id.btnDelete,piClose);
remoteViews.SetOnClickPendingEvent(R.id.BTN上一个,上一个);
}
私有无效注册表RemoteClient(){
//使用media button API(如果可用)注册media button
//事件
MediaButtonHelper.RegisterMediaButtonVentreceiveCompat(mAudioManager,MMeDiaButtonReceiveComponent);
//使用遥控器API(如果可用)设置播放状态
if(remoteControlClientCompat==null){
意向意向=新意向(意向.行动)
NotificationTarget notificationTarget = new NotificationTarget(  
    context,
    remoteView,
    R.id.iv_album_art,
    notification,
    NOTIFICATION_ID);
    Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
        mediaitem.getAlbumId());

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