《Android编程权威指南》第二版 第五章 挑战练习

5.5挑战练习

  GeoQuiz应用有一些漏洞,从易到难,以下为待解决的三个漏洞:

    1.用户作弊后,可以旋转CheatActivity来清除作弊痕迹。

    2.作弊返回后,用户可以旋转QuizActivity来消除mIsCheater变量值。

    3.用户可以不断单击NEXT按钮,跳到偷看过答案的问题,从而使作弊记录丢失。

首选需要在CheatActivity里重写onSaveInstanceState(Bundle saveInstanceState)方法

@Override
    public void onSaveInstanceState(Bundle saveInstanceState){
        super.onSaveInstanceState(saveInstanceState);
        Log.d(TAGG,"onSaveInstanceState");
        saveInstanceState.putBoolean(DO,mWasCheat);
    }

在这之前创建记录一个作弊痕迹的静态变量和两个标签常量

 public static boolean mWasCheat = false;

    private static final String TAGG = "CheatActivity";

    private static final String DO = "Cheat?";

之后取出saveInstanceState中的值

if (savedInstanceState != null){
            mWasCheat = savedInstanceState.getBoolean(DO,false);
        }

为了在作弊之后一直显示答案使用以下判断

if (mWasCheat){
            if (mAnswerIsTrue){
                mAnswerTextView.setText(R.string.true_button);
            }else {
                mAnswerTextView.setText(R.string.false_button);
            }
        }

现在开始修改QuizActivity

首先将之前的mIsCheater删除,并将使用的部分替换为CheatActivity.mWasCheat

修改重写的onSaveInstanceState()中的代码

 @Override
    public void onSaveInstanceState(Bundle saveInstanceState){
        super.onSaveInstanceState(saveInstanceState);
        Log.d(TAG,"onSaveInstanceState");
        saveInstanceState.putInt(KEY_INDEX,mCurrentIndex);
        saveInstanceState.putBoolean(WAS_CHEAT,CheatActivity.mWasCheat);
    }

取出如下

if(savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
            CheatActivity.mWasCheat = savedInstanceState.getBoolean(WAS_CHEAT,false);
        }

现在还剩最后一部:重写onDestory()方法,使得应用销毁时将mWasCheat初始化

 @Override
    public void onDestroy(){
        super.onDestroy();
        CheatActivity.mWasCheat = false;
        Log.d(TAG,"onDestroy() called");
    }

贴上全部代码:
package com.example.fkxx.geoquiz;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mCheatButton;
    private ImageButton mNextButton;
    private ImageButton mPrivButton;
    private TextView mQuestionTextView;
    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "index";
    private static final String WAS_CHEAT = "cheat";
    private static final int REQUEST_CODE_CHEAT = 0;

    private Question[] mQuestionBank = new Question[]{
            new Question(R.string.question_oceans,true),
            new Question(R.string.question_mideast,false),
            new Question(R.string.question_asia,false),
            new Question(R.string.question_americas,true),
            new Question(R.string.question_africa,true),
    };

    private void updateQuestion(){
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }

    private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;

        if (CheatActivity.mWasCheat){
            messageResId = R.string.judgment_toast;
        }
        else {
        if (userPressedTrue == answerIsTrue){
            messageResId = R.string.correct_toast;
        }else {
            messageResId = R.string.incorrect_toast;
        }}
        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
    }

    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);

        mTrueButton = (Button)findViewById(R.id.true_button);
        mFalseButton = (Button)findViewById(R.id.false_button);
        mNextButton = (ImageButton) findViewById(R.id.next_button);
        mPrivButton = (ImageButton) findViewById(R.id.priv_button);
        mCheatButton = (Button)findViewById(R.id.cheat_button);
        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //start cheatActivity
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
//                Intent i = new Intent(QuizActivity.this,CheatActivity.class);
                Intent i = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
//                startActivity(i);
                startActivityForResult(i,REQUEST_CODE_CHEAT);
            }
        });

        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(true);
            }
        });
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//            Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
                checkAnswer(false);
            }
        });
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mCurrentIndex = (mCurrentIndex + 1)%mQuestionBank.length;
//                int question = mQuestionBank[mCurrentIndex].getTextResId();
//                mQuestionTextView.setText(question);
                updateQuestion();
            }
        });
        mPrivButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCurrentIndex<1){
                    mCurrentIndex+=mQuestionBank.length;
                }
                mCurrentIndex = (mCurrentIndex - 1)%mQuestionBank.length;
                updateQuestion();
            }
        });
        if(savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
            CheatActivity.mWasCheat = savedInstanceState.getBoolean(WAS_CHEAT,false);
        }

        updateQuestion();
    }
    @Override
    public void onSaveInstanceState(Bundle saveInstanceState){
        super.onSaveInstanceState(saveInstanceState);
        Log.d(TAG,"onSaveInstanceState");
        saveInstanceState.putInt(KEY_INDEX,mCurrentIndex);
        saveInstanceState.putBoolean(WAS_CHEAT,CheatActivity.mWasCheat);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(resultCode != Activity.RESULT_OK){
            return;
        }
        if (requestCode == REQUEST_CODE_CHEAT){
            if (data == null){
                return;
            }
            Log.d(TAG,"onActivityResult");
            CheatActivity.mWasCheat = CheatActivity.wasAnswerShown(data);
        }
    }

    @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,"onSResume() called");
    }
    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        CheatActivity.mWasCheat = false;
        Log.d(TAG,"onDestroy() called");
    }
}


package com.example.fkxx.geoquiz;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends AppCompatActivity {

    private boolean mAnswerIsTrue;

    private TextView mAnswerTextView;

    private Button mShowAnswer;

    public static boolean mWasCheat = false;

    private static final String TAGG = "CheatActivity";

    private static final String DO = "Cheat?";

    private static final String EXTRA_ANSWER_IS_TRUE ="com.example.fkxx.geoquiz.answer_is_true";

    private static final String EXTRA_ANSWER_SHOWN = "com.example.fkxx.geoquiz.answer_shown";

    public static Intent newIntent(Context packageContent,boolean answerIsTrue){
        Intent i = new Intent(packageContent,CheatActivity.class);
        i.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
        return i;
    }
    public static boolean wasAnswerShown(Intent result){
        mWasCheat = result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);

    }
    @Override
    public void onSaveInstanceState(Bundle saveInstanceState){
        super.onSaveInstanceState(saveInstanceState);
        Log.d(TAGG,"onSaveInstanceState");
        saveInstanceState.putBoolean(DO,mWasCheat);
    }
    @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.answerTextView);

        mShowAnswer = (Button)findViewById(R.id.showAnswerButton);

        mShowAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAnswerIsTrue){
                    mAnswerTextView.setText(R.string.true_button);
                }else {
                    mAnswerTextView.setText(R.string.false_button);
                }
                mWasCheat = true;
                setAnswerShownResult(true);
            }

        });if (savedInstanceState != null){
            mWasCheat = savedInstanceState.getBoolean(DO,false);
        }
        if (mWasCheat){
            if (mAnswerIsTrue){
                mAnswerTextView.setText(R.string.true_button);
            }else {
                mAnswerTextView.setText(R.string.false_button);
            }
        }

    }
    private void setAnswerShownResult(boolean isAnswerShown){
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown);
        setResult(RESULT_OK,data);
    }
}

因为博主才学疏浅导致整个处理方式比较粗糙,还望谅解。




你可能感兴趣的:(《Android编程权威指南》)