检查的onStop与用户的交互,或屏幕变暗称为 [英] Check if onStop is called from user interaction, or screen dimming

查看:171
本文介绍了检查的onStop与用户的交互,或屏幕变暗称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个媒体播放器,停止播放时,用户关闭应用程序,无论是pressing home键,使用后退按钮或干脆打开另一个应用程序。 为了获得这种行为,我添加了一个的onStop()来我的主要活动,它告诉我的的MediaPlayer (这是一个服务)停止播放音乐。

I have a media player that stops playing whenever the user closes the app, either by pressing the home button, using the back button or simply opening another app. To get this behavior, I added an onStop() to my main activity which tells my MediaPlayer(which is in a service) to stop playing music.

不过,我想音乐继续玩,每当屏幕被调暗,通过使用电源按钮关闭屏幕,或只是在屏幕自动调光。

However, I would like the music to keep playing whenever the screen gets dimmed, either by using the power button to turn the screen off, or just by the screen auto dimming.

现在玩家还停止播放时,屏幕会变暗,这意味着的onStop()方法也被调用即可。

Right now the player also stops playing when the screen dims, meaning that the onStop() method also gets called then.

我如何检查的onStop()被调用的屏幕低鸣?

How can I check if the onStop() gets called by the screen diming?

我已经应用了 PARTIAL_WAKELOCK 我的的MediaPlayer 对象,尽我所知,应该让可能的球员保持运行后,屏幕熄灭。

I already applied a PARTIAL_WAKELOCK to my MediaPlayer object, which to the best of my knowledge, should make it possible for the player to keep running after the screen goes off.

我需要部分wakelock添加到我的主要活动呢?

Do I need to add a partial wakelock to my main activity as well?

刚刚申请一个 PARTIAL_WAKELOCK 来我的两个主要活动,以及我的媒体播放器。 眼下,屏幕本身不关闭了,而当用户presses电源按钮,音乐还是停止。

Just applied a PARTIAL_WAKELOCK to both my main activity, as well as my media player. Right now, the screen doesn't turn off by itself anymore, and when the user presses the power button the music still stops.

显然,这并不像我想象它的工作。

Obviously, this doesn't work as I thought it does.

有没有办法实现我要找的行为?

Is there any way of achieving the behavior I'm looking for?

推荐答案

您可以添加

private boolean stoppedByUser = false;

字段,你的活动,将其设置为 ONSTART(),到真正的 onBack pressed() onUserLeaveHint ()并检查它的价值的onStop()方法。

field to your activity, set it to false in onStart(), to true in onBackPressed() and onUserLeaveHint() and check it's value in onStop() method.

这篇关于检查的onStop与用户的交互,或屏幕变暗称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆