如何在remoteViews中使用Glide?

2024-01-12

我正在使用 Glide 从服务器加载所有图像,但我正在尝试以正确的方式将它们设置为通知和 RemoteControlClientCompat (带有锁定屏幕的很酷的东西)。我正在开发一个音乐播放器,因此每次更改歌曲时,通知中的歌曲封面都必须更改。我有这段代码,第一次可以工作(尽管图像是从 url 或可绘制对象加载的),但第二次调用时就不行了。头像没变!当歌曲更改时,将调用 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);
}

您需要设置使用通知目标 https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/request/target/NotificationTarget.java类将通知图像设置为滑动目标

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

然后以通常的滑行方式使用该目标

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

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

Glide 指南对此进行了解释

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

您可能还想制作专辑封面变化的动画 - 其描述如下:-

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在remoteViews中使用Glide? 的相关文章

随机推荐

  • 如何从 JavaScript 中的 foreach 循环中删除特定数组元素

    var fruit apple pear pear pear banana 如何从该数组中删除所有 梨 水果 我尝试了以下方法 但仍然留下一个梨 for var f in fruit if fruit f pear fruit splice
  • 动态添加边缘 visjs

    谁能帮我在这个 visjs 网络中动态添加边 实际上 我正在尝试使用拖放将节点添加到画布 但是当我单击节点并将边缘动态添加到画布上现有的另一个节点时 我需要帮助添加边缘 您可以使用 vis js 的 更新 函数动态添加节点或边 您只需传入一
  • 禁用 WebAPI 的 Windows 身份验证

    我正在使用 MVC4 应用程序并使用 WebAPI 来获取 发送我的所有数据 在控制器中 我使用 HttpClient 请求来获取数据 一切正常 我面临的问题是 当在项目中启用 Windows 身份验证时 Web API 调用将返回 401
  • 如何解决错误: getSharedPreferences(String, int) 对于 new View.OnClickListener(){} 类型未定义

    我在编码中遇到此错误 但不完全确定如何解决此问题 我已经尝试尝试解决此问题 但似乎找不到任何有效的方法 我以前做过这个 但从来没有在片段中做过 所以也许是因为这个 我正在关注exception new View OnClickListene
  • 如何在无服务器框架中为多个 dynamodb 表定义 iamrolestatements 资源?

    我想在我的无服务器项目中使用多个 dynamodb 表 如何在 iamrolestatements 中正确定义多个资源 我有一个例子serverless yml service serverless expense tracker fram
  • 作为隐藏类加载时,Lambda 表达式和匿名类不起作用

    我正在尝试在运行时编译和加载动态生成的 Java 代码 由于 ClassLoader defineClass 和 Unsafe defineAnonymousClass 在这种情况下都有严重的缺点 我尝试使用隐藏类 https openjd
  • 壶 '?'不工作表输入步骤

    我想从数据库中获取所有表名 然后从表中获取所有行 所以我创建了这样的转换 获取表名称 添加数据库连接并将表名称存储在名为 tablename 的输出字段中 表输入 标记为 替换脚本中的变量 和 对每行执行 添加了 从步骤插入数据 中的第一步
  • Pickling cv2.KeyPoint 会导致 PicklingError

    我想搜索给定目录中所有图像中的冲浪并保存它们的关键点和描述符以供将来使用 我决定使用pickle 如下所示 usr bin env python import os import pickle import cv2 class Frame
  • 如何在 html 中使用纯 javascript 切换类

    我有一个 div 我想在悬停时切换它的类 这是我的代码 function a this classList toggle first this classList toggle sec document querySelector cont
  • 如何将 R 历史记录中指定行数保存到文件中?

    这有点令人沮丧 但我确信有一个简单的答案 history max show N 将在终端上显示 N 行历史记录 savehistory file 将根据某些环境变量将多行历史记录保存到文件中 我想做的是 savehistory file m
  • 如何在Python中使用paramiko库发送箭头键?

    我正在使用 python 2 7 和代码 ssh 客户端paramiko图书馆 我用myhost channel send chr keycode 将每个键码发送到服务器 但它仅适用于 1 字节键码 我想发送其他多字节键码 例如箭头键 我怎
  • 在 Android 网页中禁用输入焦点缩放

    这是一个困境 我有一个网页 仅适用于 Android 设备 在该页面中我有一个输入框 特别是文本框 当它获得焦点时 浏览器会放大 我不希望它放大 声音容易 对吧 这就是有趣的地方 我必须能够总体缩放 所以不要说 那对我不起作用 另外 输入框
  • 控制反转和 RAII 可以一起发挥作用吗?

    我刚刚阅读了有关控制反转 IOC 的内容 这让我很困扰 它似乎让内存管理变得很痛苦 当然 ioc 似乎主要用于垃圾收集环境 Net Java Scripting 而我关心的是非 gc 设置 我在这里担心的是 IOC 在某种程度上违背了 RA
  • 检查链表的循环性[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我需要一个方法 该方法将链表作为参数
  • Facebook 页面嵌入不适用于任何浏览器中的 iFrame

    当我尝试使用嵌入 Facebook 页面时出现错误Facebook Page Plugin https developers facebook com docs plugins page plugin https developers fa
  • 直接下载 OneDrive 文件的链接吗?

    OneDrive 可共享链接如下所示 https 1drv ms w s AqmFiI7maXrRgT7PGcK 7JyZlBco https 1drv ms w s AqmFiI7maXrRgT7PGcK 7JyZlBco 如何获得此版本
  • 匹配时如何不借用期权?

    我有以下代码 fn remove descendent mut self key K gt Option
  • 是否可以在不使用 IE 开发工具的情况下更改浏览器模式?

    我们的应用程序在 IE7 和 IE8 中运行良好 我们使用X UA Compatibleheader 强制浏览器使用 IE7 标准文档模式 这是关于在得到图片后将使用哪个渲染引擎 然而 在 IE9 中 有很多东西停止工作 在IE9中 文档模
  • 使用 Windows 身份验证作为 Intranet 应用程序的登录凭据

    我正在使用 PHP 开发一个 Intranet Web 应用程序 我正在尝试使用 Windows NT 登录凭据登录到该应用程序 我在这里遇到的麻烦是如何获取远程用户的 Windows 用户名 我想要获取用户名 然后检查各个 LDAP 组
  • 如何在remoteViews中使用Glide?

    我正在使用 Glide 从服务器加载所有图像 但我正在尝试以正确的方式将它们设置为通知和 RemoteControlClientCompat 带有锁定屏幕的很酷的东西 我正在开发一个音乐播放器 因此每次更改歌曲时 通知中的歌曲封面都必须更改