单击侦听器上的通知 RemoteView

2024-01-09

所以,经过一番绞尽脑汁之后,我已经无计可施了。我有一个媒体播放器RemoteViews在我的通知中,我希望能够访问播放、暂停、上一个和下一个按钮。

我知道setOnClickPendingIntent()将用于通过通知进行通信。然而,我想知道这将如何运作。

是否可以让服务处理点击?

我已经尝试过这个,但没有成功。我试图让我的服务处理播放器的暂停和恢复:

rm = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notif_media_player);

        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.putExtra(REQUEST_CODE, PAUSE);
        PendingIntent pending = PendingIntent.getService(getApplicationContext(), PAUSE, intent, 0);
        rm.setPendingIntentTemplate(R.id.pause, pending);

        Notification notif =  new NotificationCompat.Builder(getApplicationContext())
                                 .setOngoing(true)
                                 .setSmallIcon(R.drawable.ic_launcher)
                                 .setContent(rm)
                                 .build();

        NotificationManager mgr = (NotificationManager) getApplicationContext()
                                  .getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(NOTIFICATION_ID, notif);  

and my IBinder一片空白。如果这有影响的话。

如何通过通知暂停和播放音乐?


基于CommonsWare的推荐:

        rm = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notif_media_player);
        Intent pauseIntent = new Intent(getApplicationContext(), MusicService.class);
        Intent dismissIntent = new Intent(getApplicationContext(), MusicService.class);

        pauseIntent.putExtra(REQUEST_CODE, PAUSE);
        dismissIntent.putExtra(REQUEST_CODE, DISMISS);

        PendingIntent pause = PendingIntent.getService(getApplicationContext(), PAUSE, pauseIntent, 0);
        PendingIntent dismiss = PendingIntent.getService(getApplicationContext(), DISMISS, dismissIntent, 0);

        rm.setOnClickPendingIntent(R.id.pause, pause);
        rm.setOnClickPendingIntent(R.id.dismiss, dismiss);

        Notification notif =  new NotificationCompat.Builder(getApplicationContext())
                                 .setOngoing(true)
                                 .setSmallIcon(R.drawable.ic_launcher)
                                 .setContent(rm)
                                 .build();

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

单击侦听器上的通知 RemoteView 的相关文章

  • Android 覆盖在软件按钮之上

    我正在尝试编写一个绘制自定义鼠标指针的应用程序 我目前有一个服务 它创建一个扩展 ViewGroup 的类 并使用 WindowManager 系统服务将其显示为带有 FLAG LAYOUT IN SCREEN 设置的 TYPE SYSTE
  • EditText 中的验证允许 IP 或 Web Url 主机

    我需要对我的 EditText 进行验证 以便它允许我输入有效的 IP 地址格式 即示例 132 0 25 225 or 网址格式 www 例如 www example com 逻辑是 如果用户首先输入任何数值 则验证 IP 将执行操作 否
  • 单击 RecyclerView 内的 ImageView 时更改图像资源

    每当我单击它时 单击图像 而不是项目 我都会尝试更改回收器视图内特定位置的设备图像资源 我尝试将 setOnClickListener 放入 onBindViewHolder 方法中 但只有最后一个项目受到影响 这是我的回收者视图 http
  • Android中如何使用洪水填充算法?

    我是Android编程新手 最近尝试编写一个简单的应用程序 仅供练习 在这个中 我想在用户点击时为图像着色 但我不知道如何开始 我读过不同的主题 其中提到使用 洪水填充 算法 我在网上找到了它 但我不知道如何将它放入我的简单应用程序中 我找
  • 显示警报或收到通知时的视图

    我正在关注this http tokudu com 2010 how to implement push notifications for android 显示的教程通知 on an 安卓设备 当我在设备上运行该应用程序时 状态栏上会出现
  • Android Studio:lambda 不起作用[重复]

    这个问题在这里已经有答案了 当尝试使用 lambda 表达式时 我遇到了一些 Gradle 构建错误 错误 41 100 错误 source 1 7 不支持 lambda 表达式 使用 source 8 或更高版本来启用 lambda 表达
  • Google Firebase - 如何删除崩溃报告?

    我最终失明了吗 还是没有明显的方法可以通过 Google Firebase Web 控制台删除 Firebase 崩溃报告 我的 Android 应用程序已成功记录报告 但现在出现大量 开发崩溃 占用了我在控制台中的大部分视图 这使得找到实
  • 为什么按钮上的 maxWidth 不起作用以及如何解决它?

    我的布局上有两个按钮 在大屏幕设备 平板电脑 上我想限制它们的宽度 这样它们看起来就不会很荒谬 我希望使用 maxWidth 属性 但它显然在我的场景中没有任何作用 这是布局定义 按钮使用布局的整个宽度 忽略 maxWidth 中的任何值
  • Android中不同线程的数据库访问

    我有一个在 AsyncTasks 中从互联网下载数据的服务 它解析数据并将其存储在数据库中 该服务持续运行 当服务写入数据库时 活动会尝试从数据库中读取更改 我有一个数据库助手 有多种写入和读取方法 这会导致问题吗 可能尝试从两个不同的线程
  • Android ListView setSelection() 似乎不起作用

    我有一个ListActivity实现onListItemClick 并调用doSomething 类的功能 后者包含l setSelection position where l is the ListView object 现在有一个on
  • window.onbeforeunload 在 Android Chrome 上不会触发 [alt.解决方案?]

    我开发了一个简单的聊天应用程序 我正在使用 window onbeforeunload当有人关闭选项卡 浏览器时 基本上是当用户离开房间时 通知其他用户 这是我的代码 scope onExit function scope chatstat
  • 如何从android获取应用程序安装时间

    我尝试了一些方法 但没有成功 请帮助我 PackageManager pm context getPackageManager ApplicationInfo appInfo pm getApplicationInfo app packag
  • Android 改变 ImageView / Bitmap 的颜色

    我需要找到一种方法来改变 Android 中位图的颜色 我需要在我的应用程序中平滑地替换 更改椭圆形图像的颜色 具体取决于int价值 我需要类似的东西myValue 5比改变我的图像的颜色RED and if myValue 322将颜色更
  • 如何在android中的操作栏中创建Edittext?

    我们可以在操作栏中使用编辑文本吗 在阅读了 Google 中的大量资源后 我找不到如何在操作栏中创建编辑文本 谁能告诉我该怎么做 您可以设置自定义View为了ActionBar像这样 getActionBar setCustomView R
  • android 中camera.setParameters 失败

    我已将相机功能包含在我的应用程序中 我还在市场上推出了该应用程序 我从一位用户那里收到一条错误消息 称他在打开相机时遇到错误 我已经在 2 1 的设备上测试了该应用程序 我从用户那里得到的错误是使用 Nexus One 它主要运行 2 2
  • 使用嵌套的 hashmap 参数发送 volley 请求

    我正在使用 android volley 框架向我的服务器发送 jsonobject 请求 get 请求工作正常 现在我想发送一个带有请求参数的 post 请求 该请求参数是嵌套的 hashmap 我重写 getparams 方法 但它期望
  • 在 Android 中上传文件出现内存不足错误

    我的上传代码如下 String end r n String twoHyphens String boundary try URL url new URL ActionUrl HttpURLConnection con HttpURLCon
  • Android:无法发送http post

    我一直在绞尽脑汁试图弄清楚如何在 Android 中发送 post 方法 这就是我的代码的样子 public class HomeActivity extends Activity implements OnClickListener pr
  • 通过powershell运行ADB命令

    所以我尝试通过 powershell 脚本运行一些 ADB 命令 这是我正在尝试做的一个简单示例 adb shell echo in adb shell su root echo you are now root ls cd data da
  • Android ADT Eclipse 插件,parseSDKContent 失败

    我刚刚设置了我的第一个 Android 开发环境 其中包括 日食3 5 Mac OS X 10 5 适用于 x86 mac 的 Android SDK ADT Eclipse 插件 0 9 6 我已将 set PATH 设置为我的 SDK

随机推荐