我的 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/