当Home键pressed的Andr​​oid停止背景音乐背景音乐、pressed、Home、oid

2023-09-12 05:42:42 作者:久了就旧了

我开发一个应用程序,它有多个活动。用户可以浏览到的任何活动。我开始从第一个主要活动的背景音乐,它一直播放整个应用程序。现在我想,只要用户presses HOME键,媒体播放器应该暂停播放,当用户回来的应用程序,它开始再次播放。首先,我做的媒体播放器的静态,并暂停音乐的onPause()和onResume()打不过它创建了一个混蛋,而活动之间的切换。我希望你有我的观点。任何想法如何暂停时,HOME键pressed玩,玩一遍,当用户回来?

I am developing an app which has multiple activities. User can navigate to any activity. I start background music from first main activity and it keeps playing throughout the application. Now I want that whenever user presses HOME key, the media player should pause playing and when user comes back to app, it starts playing again. First I made media player static and was pausing music in onPause() and playing in onResume() but it creates a jerk while switching between activities. I hope you got my point. Any idea how to pause playing when HOME key pressed and play it again when user comes back?

推荐答案

看看的 Activity.onUserLeaveHint() 的http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()

古称活动周期的一部分,当活动即将   进入背景作为用户选择的结果。例如,当   用户presses Home键,onUserLeaveHint()将被调用,但   当电话呼叫到来使得在呼叫活动是   自动被推上前台,onUserLeaveHint()将不   呼吁活动被中断。在情况下,当它被调用时,   这种方法活动的的onPause()回调之前,右边的叫。

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.