当屏幕旋转时,用户可以欺骗Android

时间:2014-07-29 07:56:32

标签: java android android-activity toast

我正在关注Big Nerd Ranch指南Android,其中我正在构建一个基本应用程序,它显示一组问题,你说的是真或假。有一个按钮“Cheat”,您可以在其中作弊。如果您点击按钮Cheat看到答案,请返回并输入正确答案,然后显示toast显示“作弊错误”。这在垂直屏幕上工作正常,但我可以说我将屏幕旋转到水平,我查看答案并回到水平,我可以作弊。我不希望这种情况发生。

我的努力: 使用onSavedInstanceState覆盖现有功能以在屏幕和屏幕之间共享值跟踪onCreate

中的值
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mAnswerIsTrue) ;
}

onCreate方法中,

if(savedInstanceState !=null)
{
 mAnswerIsTrue = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN);
 setAnswerShownResult(mAnswerIsTrue);
}

这是我的代码:

package com.android.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity 
{
    //Extras
    public static final String EXTRA_ANSWER_IS_TRUE = "com.android.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "com.android.geoquiz.answer_shown";

    private static final String TAG="CheatActivity";
    private static final String KEY_INDEX = "cheat";
    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private Button mShowAnswer;

    private void setAnswerShownResult(boolean isAnswerShown)
    {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);


        mAnswerIsTrue =  getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        mAnswerTextView = (TextView)findViewById(R.id.showAnswerButton);
        setAnswerShownResult(false);
        mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
        mShowAnswer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    if(mAnswerIsTrue)
                    {
                        mAnswerTextView.setText(R.string.true_button);
                    }
                    else
                    {
                        mAnswerTextView.setText(R.string.false_button);
                    }
                    setAnswerShownResult(true);
            }
        });



        if(savedInstanceState !=null)
        {

            mAnswerIsTrue = savedInstanceState.getBoolean(KEY_INDEX, false);

        }

    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putBoolean(KEY_INDEX, mAnswerIsTrue) ;
    }


    @Override
    public void onStart()
    {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }
    @Override
    public void onPause()
    {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }
    @Override
    public void onResume()
    {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }
    @Override
    public void onStop()
    {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
    }


}

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为一旦用户旋转屏幕就会调用onCreate,有一个简单的解决方案可以阻止onCreate被调用onOrientationChanged

<activity android:name=".MyActivity" 
          android:configChanges="orientation|keyboardHidden" />

现在当你这样做时,你必须覆盖你的活动类中的方法,就像你有onCreate,onPause你需要添加另一个名为onConfigChanged()的方法; 现在,无论何时旋转屏幕,都会触发此方法,而不是一次又一次地创建活动。

将此添加到您的活动类

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

希望这会有所帮助。如果您需要任何帮助,请随时提出。

了解更多信息,请阅读这些帖子。

Activity restart on rotation Android

How do I disable orientation change on Android?

http://developer.android.com/guide/topics/manifest/activity-element.html#config

答案 1 :(得分:0)

在您的清单中将此添加到您的活动中

android:configChanges="orientation|screenSize"

并在Activity侦听配置更改

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    System.out.println(newConfig.toString());
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         Toast.makeTest(this,"portrait",Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeTest(this,"landscape",Toast.LENGTH_SHORT).show();
    }

}