来自通知栏的Android调用方法

来自通知栏的Android调用方法,android,android-intent,android-mediaplayer,android-notifications,android-4.2-jelly-bean,Android,Android Intent,Android Mediaplayer,Android Notifications,Android 4.2 Jelly Bean,我的android MainActivity.java类中有以下(简化)代码 case R.id.menu_stop: stopPlaying(); return true; 在果冻豆通知中按下操作按钮时,如何使用意图调用stopPlaying()?该通知构建在我的StreamingService.java类中。我可以使用动作按钮来触发电子邮件、电话和短信,我只是不知道如何在代码中调用方法 我在stopPlaying()方法中的代码如下 private void stopPlayi

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

case R.id.menu_stop:
    stopPlaying();
  return true;
在果冻豆通知中按下操作按钮时,如何使用意图调用
stopPlaying()
?该通知构建在我的StreamingService.java类中。我可以使用动作按钮来触发电子邮件、电话和短信,我只是不知道如何在代码中调用方法

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

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

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

以下是一个很好的入门教程:


基本上,您需要编写一个接收器并将其注册到您的服务中。然后,当按下通知中的按钮时,您希望在服务的接收器等待时停止广播(这需要一个意图)。当它接收到广播(基本上是即时的)时,它可以调用stopPlaying()方法。

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

以下是一个很好的入门教程:

基本上,您需要编写一个接收器并将其注册到您的服务中。然后,当按下通知中的按钮时,您希望在服务的接收器等待时停止广播(这需要一个意图)。当它接收到广播(基本上是即时的)时,它可以调用stopPlaying()方法