1

I have an activity in my android app that starts a service and it also reacts to button presses. I have a mediaplayer that is instantiated and running in the android service. the plan is to take the button press event in the activity and transmit that information to the service that is running media player in it. That way the pause button press will send the button press event info to the service and in the service the pause method for media player will be called.

Two ideas I can think of now, the first is create a broadcast receiver class and register the button presses in that class and send them to the service with an intent extra. the second idea is to make a nested anonymous inner class of the broadcast receiver type and handle that inside of the service.

What is the best way to do this?

4

1 回答 1

1

只要您想传递数据,只需调用 startService(..) 即可。该服务将第一次运行,但下一次调用只会触发新的 onStartCommand() 调用以及您传递的新附加内容。

于 2017-07-28T06:42:20.340 回答