活动onPause奇怪的行动

时间:2012-06-06 20:59:43

标签: android onresume onpause

我的活动中有一个onPause。奇怪的是,只有单个活动(一个videoPlayer)onPause被调用两次

方案: 这种情况发生在我的手机设备上的视频播放器中,而不是平板电脑。

按电源按钮进入待机状态,调用暂停,然后调用onresume,然后再次调用暂停。然后再次按下电源按钮,恢复就像它应该的那样被调用。

有没有人遇到过这样的问题,如果有的话,你是怎么解决它的?即使只是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;

        }
    }

我会保存一些布局的状态,比如记录器布局暂停,而它有点像菜单。然后,如果按下电源,它会转动,但当它重新打开时,布局就会消失,视频正在播放,这是不应该发生的。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

虽然这是一个非常晚的答案,但我只想分享我的经验。我遇到了类似的情况,但这里的主要问题是由于视频播放器(可能在横向播放视频)或者您的活动目前在景观中而导致的方向改变。因此,当您在设备实际生命周期阶段按下电源关闭按钮时:onPause(),onStop(),然后更改方向(默认锁定屏幕为纵向),这会导致调用onCreate(),onStart(),onPause( )...

解决方案很简单:通过指定android:configChanges =" orientation | screenSize"手动处理方向更改清单中的活动定义中的atribbute和重写java类中的onConfigurationChanged()