2013-09-26 30 views
1

在我的Android測驗應用程序中,我實施了Admob。但每次給出答案並且問題改變時,Admob也會刷新,這需要大約3-4秒。但到那個時候,問題又會改變,等等。Android:活動循環導致廣告刷新

QuestionActivity.class:

package com.tsc.walkingdead; 

import java.util.List; 
import com.google.ads.*; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.TextView; 
import android.widget.Toast; 



import com.tsc.walkingdead.R; 
import com.tsc.walkingdead.quiz.GamePlay; 
import com.tsc.walkingdead.quiz.Question; 
import com.tsc.walkingdead.util.Utility; 


public class QuestionActivity extends Activity implements OnClickListener{ 

    private Question currentQ; 
    private GamePlay currentGame; 
; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.question); 

// Look up the AdView as a resource and load a request. 
AdView adView = (AdView)this.findViewById(R.id.adView); 
adView.loadAd(new AdRequest()); 

     /** 
     * Configure current game and get question 
     */ 
     currentGame = ((WalkingDeadApplication)getApplication()).getCurrentGame(); 
     currentQ = currentGame.getNextQuestion(); 
     Button nextBtn = (Button) findViewById(R.id.nextBtn); 
     nextBtn.setOnClickListener(this); 

     /** 
     * Update the question and answer options.. 
     */ 
     setQuestions(); 

    } 


    /** 
    * Method to set the text for the question and answers from the current games 
    * current question 
    */ 
    private void setQuestions() { 
     //set the question text from current question 
     String question = Utility.capitalise(currentQ.getQuestion()) + "??"; 
     TextView qText = (TextView) findViewById(R.id.question); 
     qText.setText(question); 

     //set the available options 
     List<String> answers = currentQ.getQuestionOptions(); 
     TextView option1 = (TextView) findViewById(R.id.answer1); 
     option1.setText(Utility.capitalise(answers.get(0))); 

     TextView option2 = (TextView) findViewById(R.id.answer2); 
     option2.setText(Utility.capitalise(answers.get(1))); 

     TextView option3 = (TextView) findViewById(R.id.answer3); 
     option3.setText(Utility.capitalise(answers.get(2))); 

     TextView option4 = (TextView) findViewById(R.id.answer4); 
     option4.setText(Utility.capitalise(answers.get(3))); 
    } 


    @Override 
    public void onClick(View arg0) { 
     //Log.d("Questions", "Moving to next question"); 

     /** 
     * validate a checkbox has been selected 
     */ 
     if (!checkAnswer()) return; 


     /** 
     * check if end of game 
     */ 
     if (currentGame.isGameOver()){ 
      Intent i = new Intent(this, EndgameActivity.class); 
      startActivity(i); 
      finish(); 
     } 
     else{ 
      Intent i = new Intent(this, QuestionActivity.class); 
      startActivity(i); 
      finish(); 
     } 
    } 


    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     switch (keyCode) 
     { 
     case KeyEvent.KEYCODE_BACK : 
      return true; 
     } 

     return super.onKeyDown(keyCode, event); 
    } 


    /** 
    * Check if a checkbox has been selected, and if it 
    * has then check if its correct and update gamescore 
    */ 
    private boolean checkAnswer() { 
     String answer = getSelectedAnswer(); 
     if (answer==null){ 
      //Log.d("Questions", "No Checkbox selection made - returning"); 
      return false; 
     } 
     else { 
      //Log.d("Questions", "Valid Checkbox selection made - check if correct"); 
      if (currentQ.getAnswer().equalsIgnoreCase(answer)) 
      { 
       //Log.d("Questions", "Correct Answer!"); 
       currentGame.incrementRightAnswers(); 
       Toast.makeText(this, "Correct!", Toast.LENGTH_LONG).show(); 
      } 

      else{ 
       //Log.d("Questions", "Incorrect Answer!"); 
       currentGame.incrementWrongAnswers(); 
       Toast.makeText(this, "Wrong!", Toast.LENGTH_LONG).show(); 
      } 
      return true; 
     } 
    } 


    /** 
    * 
    */ 
    private String getSelectedAnswer() { 
     RadioButton c1 = (RadioButton)findViewById(R.id.answer1); 
     RadioButton c2 = (RadioButton)findViewById(R.id.answer2); 
     RadioButton c3 = (RadioButton)findViewById(R.id.answer3); 
     RadioButton c4 = (RadioButton)findViewById(R.id.answer4); 
     if (c1.isChecked()) 
     { 
      return c1.getText().toString(); 
     } 
     if (c2.isChecked()) 
     { 
      return c2.getText().toString(); 
     } 
     if (c3.isChecked()) 
     { 
      return c3.getText().toString(); 
     } 
     if (c4.isChecked()) 
     { 
      return c4.getText().toString(); 
     } 

     return null; 
    } 

} 

question.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/walking_dead_background2" 
     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:gravity="center" 
    android:orientation="vertical" 
    > 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center"> 

     <RadioGroup 
      android:id="@+id/group1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/question" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       style="@style/myStyle1" 
       android:layout_marginBottom="10dp"/> 

      <RadioButton 
       android:id="@+id/answer1" 
       android:checked="false" 
       style="@style/myStyle"/> 

      <RadioButton 
       android:id="@+id/answer2" 
       android:checked="false" 
       style="@style/myStyle"/> 

      <RadioButton 
       android:id="@+id/answer3" 
       android:checked="false" 
       style="@style/myStyle"/> 

      <RadioButton 
       android:id="@+id/answer4" 
       android:checked="false" 
       style="@style/myStyle"/> 

        <Button 
         android:id="@+id/nextBtn" 
         style="@style/myStyle" 
         android:layout_width="80dip" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:background="@drawable/button_selector" 
         android:paddingBottom="5dip" 
         android:paddingTop="5dip" 
         android:text="Next" 
         android:textColor="#ffffff" /> 

     </RadioGroup> 

</LinearLayout> 
<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     android:id="@+id/mainLayout"> 
    <com.google.ads.AdView android:id="@+id/adView" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         ads:adUnitId="ca-app-pub-6195077402939893/2503837566" 
         ads:adSize="BANNER" 
         ads:testDevices="TEST_EMULATOR"/> 




</LinearLayout> 
</LinearLayout> 

是否有可能讓廣告顯示完整的時間,但這些問題正在發生變化?

謝謝

+0

我想你應該更好地加載這些問題的片段和保持AdMob廣告僅在父活動,這樣一來,每次它不會被重新加載。 –

回答

0

您的問題是您正在#onClick中開始新的活動。 不要開始一個新的活動,重新使用當前的活動。

在這種情況下somethig像:

@Override 
public void onClick(View arg0) { 
    //Log.d("Questions", "Moving to next question"); 

    /** 
    * validate a checkbox has been selected 
    */ 
    if (!checkAnswer()) return; 


    /** 
    * check if end of game 
    */ 
    if (currentGame.isGameOver()){ 
     Intent i = new Intent(this, EndgameActivity.class); 
     startActivity(i); 
     finish(); 
    } 
    else{ 
     currentQ = currentGame.getNextQuestion(); 
     setQuestions(); 
    } 
} 
+0

你能告訴我該怎麼做嗎?我很新的Android編程.... – user2781086

+0

而不是startActivity(新的意圖(this,QuestionActivity.class));在#onClick中,只需更新當前活動中的問題即可。 – William

+0

是的,但如何?你能給我一個代碼嗎? – user2781086

0

您嘗試從您的onCreate刪除

adView.loadAd(new AdRequest()); 

並添加

ads:loadAdOnCreate="true" 

到您的廣告XML。

+0

已經嘗試過了,幫不了 – user2781086