gpt4 book ai didi

android - Activity onPause 奇怪的 Action

转载 作者:太空狗 更新时间:2023-10-29 12:52:25 25 4
gpt4 key购买 nike

我的 Activity 中有一个 onPause。奇怪的是,仅在单个 Activity (videoPlayer)中,onPause 就被调用了两次

场景:这种情况发生在我的手机设备上的视频播放器中,但平板电脑上没有。

按电源按钮进入待机状态,调用 onpause 然后调用 onresume 然后再次调用 on pause。然后再次按下电源按钮,并按预期调用恢复。

有没有人遇到过这样的问题,如果有,你是如何解决的。甚至只是让 onPause onResume onpause 然后打开 onResume。

提前致谢。这个出现了两次

    protected void onPause()
{

Log.i("VideoPlayer", "onPauseCalled");
super.onPause();
pauseMedia();
Log.i("onPause", " save states to be called");
if(saveAllowed)
saveStates();
Log.i("onPause", " save States called");
view.setVisibility(View.GONE);
//Log.i("onPause", "visibility GOne");
removeListeners();
doCleanUp();


}

@Override
protected void onResume()
{
super.onResume();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Log.i("VideoPlayer", "onResumeCalled");

if(pm.isScreenOn())
{
//Initialization
initializeViewControls();
handler = new Handler();

initializeButtons();
initRecordButtons();
initVolumeControl();

//RestoreState
restoreMediaBoolean();
restoreMediaState();
Log.i("After", "restoreState");
view = (SurfaceView) findViewById(R.id.surfaceView);
//Log.i("After", "GettingView");
holder = view.getHolder();
view.setVisibility(View.VISIBLE);
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Log.i("onResume", "view is Visible");
saveAllowed = true;
}
else
saveAllowed = false;

}

private void saveStates()
{
if(!isFinishing)
{
//Log.i("VideoPlayer", "Called at saveStates()");

prefs = getSharedPreferences(PREF_SAVE, Context.MODE_PRIVATE);
if(prefs != null)
{
prefEditor = prefs.edit();
}

if(prefEditor != null)
{

//Save overlay visibiltiy
if((volumeLayout != null) && (volumeLayout.getVisibility() == View.VISIBLE))
{
prefEditor.putBoolean(IS_VOLUME_VISIBLE, true);
prefEditor.commit();
}
else
{
prefEditor.putBoolean(IS_VOLUME_VISIBLE, false);
prefEditor.commit();
}

//Save recorder Overlay state
if((recordLayout != null) && (recordLayout.getVisibility() == View.VISIBLE) )
{
prefEditor.putBoolean(IS_RECORDER_VISIBLE, true);
prefEditor.commit();
}
else
{
prefEditor.putBoolean(IS_RECORDER_VISIBLE, false);
prefEditor.commit();
}
//Save controller OVerlay State
if((backButton != null) && (backButton.getVisibility() == View.VISIBLE))
{

prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, true);
prefEditor.commit();
Log.i("Save States", "Controller saved as visible");
}
else
{
Log.i("Save States", "Controller saved as Invisible");
//Log.i("Saving", "Controller Invisible");
prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, false);
prefEditor.commit();
}

//Save is private Audio Recorded
prefEditor.putBoolean(IS_PRIVATE_AUDIO_RECORDED, isCustomVoiceRecorded);
prefEditor.commit();

//Save are we playing out custom audio
prefEditor.putBoolean(PLAY_CUSTOM_AUDIO, playCustomAudio);
prefEditor.commit();

//Make boolean is restoring
prefEditor.putBoolean(IS_RESTORING, true);
prefEditor.commit();

//Saves Position of Current Video
if(vidplayer != null)
{
prefEditor.putInt(VIDEO_POSITION, videoPausedAt);
prefEditor.commit();
}

//Save Position of Current Audio
if(audplayer != null)
{
prefEditor.putInt(AUDIO_POSITION, audioPausedAt);
prefEditor.commit();
}


prefEditor = null;
prefs = null;

}
}

发生的事情是我保存了一些布局的状态,比如录音机布局,它在退出时暂停,有点像菜单。然后,如果按下电源,它就会关闭,但当它重新打开时,布局消失了,视频正在播放,这是不应该发生的。

最佳答案

是的,我遇到了类似的问题。您使用的是哪种设备(如果我没有记错,我认为这是 NFC 屏幕超时问题)?暂停对我来说不是问题,但我尝试在 onResume 中恢复视频(被调用两次)所以我在 onResume 中使用了以下内容:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
//now do stuff
}

关于android - Activity onPause 奇怪的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com