3

アクティビティにonPauseがあります。奇妙なことに、その1つのアクティビティ(videoPlayer)でonPauseが2回呼び出されます

シナリオ:これは、電話デバイスのビデオプレーヤーで発生しますが、タブレットでは発生しません。

電源ボタンを押してスタンバイになり、onpauseが呼び出され、次にonresumeが呼び出され、次にonpauseが再度呼び出されます。次に電源ボタンをもう一度押すと、再開時に想定どおりに呼び出されます。

誰かがそのような問題に遭遇したことがありますか?もしそうなら、どのようにそれを修正しましたか?onPause onResume onpauseを設定してから、onResumeの電源をオンにします。

前もって感謝します。これは2回表示されます

    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;

        }
    }

何が起こるかというと、メニューのように外に出ている間一時停止するレコーダーレイアウトのようないくつかのレイアウトの状態を保存します。次に、電源を押すと電源がオフになりますが、電源を入れるとレイアウトがなくなり、ビデオが再生されますが、これは発生しないはずです。

4

2 に答える 2

3

はい、私は同様の問題を抱えていました。どのデバイスを使用していますか(メモリが正しく機能する場合、NFC画面のタイムアウトの問題だったと思います)?一時停止は私にとって問題ではありませんでしたが、onResume(2回呼び出されます)でビデオを再開しようとしているので、onResumeで次を使用しました:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
   //now do stuff
}
于 2012-06-06T21:26:34.297 に答える
0

これは非常に遅い答えですが、私は自分の経験を共有したいと思います。私は同様の状況に遭遇しましたが、ここでの主な問題は、ビデオプレーヤー(おそらく横向きでビデオを再生する)またはあなたの活動が現在横向きであるかどうかによって引き起こされる向きの変化です。したがって、デバイスの電源オフボタンを押すと、実際のライフサイクルフェーズは次のようになります:onPause()、onStop()、次に向きが変更され(ロック画面はデフォルトで縦向きになります)、onCreate()、onStart()、onPause( )..。

解決策は簡単です。マニフェストのアクティビティ定義でandroid:configChanges = "orientation | screenSize"属性を指定し、JavaクラスのonConfigurationChanged()をオーバーライドすることで、方向の変更を手動で処理します。

于 2016-05-20T13:52:41.083 に答える