来自通知栏 Intent 的 Android 调用方法

标签 android android-intent android-mediaplayer android-notifications android-4.2-jelly-bean

我的 android MainActivity.java 类中有以下(简化的)代码

case R.id.menu_stop:
    stopPlaying();
  return true;

当在 Jelly Bean 通知中按下操作按钮时,如何使用 Intent 调用 stopPlaying()?通知内置于我的 StreamingService.java 类中。我可以使用操作按钮来触发电子邮件、电话和 SMS intent,我只是不知道如何在我的代码中调用方法。

我在stopPlaying()方法中的代码如下

private void stopPlaying() {
    Intent intent = new Intent(this, StreamingService.class);
    stopService(intent);
}

最佳答案

您将需要使用广播/接收器设计模式。

这里有一个很好的入门教程:http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

基本上,您需要编写一个接收器并将其注册到您的服务中。然后,当您的通知中的按钮被按下时,您想要在您的服务接收器等待时触发广播(这需要一个 Intent )。当它收到广播时(基本上是瞬间),它可以调用您的 stopPlaying() 方法。

关于来自通知栏 Intent 的 Android 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319554/

相关文章:

java - UDP 流无法在 ExoPlayer 上播放

Android 为产品口味定制工作流程

android - 在 intent android 中传递一个数组

android - ExoPlayer 和启动/暂停/seekTo 命令

java - 如何使用 Android 动画播放 'Tick Tick' 声音(可能使用 `Android MediaPlayer/SoundPool` )?

android - 动态更改 Activity 中的查看内容

java - 应用程序正在运行,但现在意外关闭

java - MediaPlayer start()/stop() mUri 为 null

java - 如何正确使用回调接口(interface)从 onDataChange 获取 RecyclerView 的值列表?

android - 如何从小部件启动配置 Activity ?