本文主要是介绍android onResume is called before you unlock the phone,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近遇到一个问题,android上运行app当锁定屏幕,然后解锁.
android 解锁屏幕之前会先调用onResume方法,有时候这并不是开发者想要的结果...比如声音在没解锁之前就播放 用户体验效果就有点差了...
解决这个问题有两个方法,直接上代码了...
1.
- </pre><p><span style="font-family: monospace; "><span style="white-space:pre"></span></span></p><pre name="code" class="java">@Override
- public void onWindowFocusChanged(boolean hasFocus) {
- if(hasFocus)
- <span style="white-space:pre"> </span>{
- // todo something;
- }
- super.onWindowFocusChanged(hasFocus);
- }
2.
- public class ExampleActivity extends Activity {
- @Override
- protected void onCreate() {
- IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- BroadcastReceiver mReceiver = new ScreenReceiver();
- registerReceiver(mReceiver, filter);
- }
- @Override
- protected void onPause() {
- if (ScreenReceiver.wasScreenOn) {
- System.out.println("onPause() called because screen turned off.");
- } else {
- System.out.println("normal onPause() call");
- }
- super.onPause();
- }
- @Override
- protected void onResume() {
- if (!ScreenReceiver.wasScreenOn) {
- System.out.println("onResume() called when screen turns on");
- } else {
- System.out.println("normal onResume() call");
- }
- super.onResume();
- }
- }
这篇关于android onResume is called before you unlock the phone的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!