2014-03-01 91 views
1

我的活動中有一個倒數計時器。CountDownTimer完成後的第二個活動循環

當用戶回答更正時,它將加載帶有剩餘時間的相同活動。

但倒數完成後,第二個活動加載循環。

這裏是我的ArcadeActivty代碼:

public class ArcadeActivity extends Activity { 

final Context context = this; 
ImageView image; 
String icon_game; 
String[] ar; 
String name; 
String imge; 
String desc; 
String clue; 
int clue_status=0; 
int gid = 0; 
int level = 0; 
long seconds; 
final TestAdapter mDbHelper = new TestAdapter(this); 
int coin = 0; 
private Button btn_clue; 
private Button btn_skip; 
private TextView txt_coins; 
private View view; 
private MediaPlayer mp = null; 
private TextView lbl_timer; 
private CountDownTimer countDownTimer; 
long currentTime; 
int score; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_arcade); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { 
     currentTime = extras.getLong("timeCode"); 
     score = extras.getInt("myScore"); 
    } else { 
     currentTime = 180000; 
    } 

    startTimer(); 

    final Button btn_submit = (Button) findViewById(R.id.btn_submit); 
    btn_clue = (Button) findViewById(R.id.btn_clue); 
    btn_skip = (Button) findViewById(R.id.btn_skip); 
    lbl_timer = (TextView) findViewById(R.id.lbl_timer); 
    final EditText txt_answer = (EditText) findViewById(R.id.txt_answer); 


    ImageView img = (ImageView) findViewById(R.id.img_clue);   

    mDbHelper.createDatabase();  
    mDbHelper.open(); 



    Cursor testdata = mDbHelper.getArcade(); 


    name = Utility.GetColumnValue(testdata, "name"); 
    imge = Utility.GetColumnValue(testdata, "image"); 
    clue = Utility.GetColumnValue(testdata, "clue"); 
    clue_status = Integer.parseInt(Utility.GetColumnValue(testdata, "clue_status")); 
    System.out.println(clue_status); 
    level = Integer.parseInt(Utility.GetColumnValue(testdata, "level_order")); 
    desc = Utility.GetColumnValue(testdata, "description"); 
    gid = Integer.parseInt(Utility.GetColumnValue(testdata, "_id")); 

    int id = getResources().getIdentifier("com.iamnards.gameapp:drawable/" + imge, null, null); 
    img.setImageResource(id); 

    //lbl_level.setText("Level "+level); 

    btn_submit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      if (name.toLowerCase().contains(txt_answer.getText().toString().toLowerCase()) && txt_answer.getText().toString().trim().length() > 0) { 
       //playSound(R.raw.complete);  
       Toast.makeText(getApplicationContext(), "Perfect! ", Toast.LENGTH_SHORT).show(); 
       //load_correct_details(imge,name,desc); 
       mDbHelper.updateArcadeStatus(gid); 
       mDbHelper.close(); 
       score = score+10; 
       Intent i = new Intent(ArcadeActivity.this, ArcadeActivity.class); 
       i.putExtra("timeCode", currentTime); 
       i.putExtra("myScore", score); 
       startActivity(i); 

      } else { 
       //playSound(R.raw.error); 
       Toast.makeText(getApplicationContext(), "Are you serious?", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    btn_skip.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(ArcadeActivity.this, ArcadeActivity.class); 
      i.putExtra("timeCode", currentTime); 
      i.putExtra("myScore", score); 
      startActivity(i); 
     } 
    }); 

} 

void startTimer() { 
    countDownTimer = new CountDownTimer(currentTime, 1000) { 
     // 500 means, onTick function will be called at every 500 
     // milliseconds 

     @Override 
     public void onTick(long leftTimeInMilliseconds) { 
      seconds = leftTimeInMilliseconds/1000; 
      currentTime = leftTimeInMilliseconds; 
      lbl_timer.setText(String.format("%02d", seconds/60) 
        + ":" + String.format("%02d", seconds % 60)); 

     } 

     @Override 
     public void onFinish() { 
      // this function will be called when the timecount is finished 
       if(currentTime<1717){ 
        Toast.makeText(getApplicationContext(), "TIMES UP! "+currentTime, Toast.LENGTH_SHORT).show(); 
        Intent i = new Intent(ArcadeActivity.this, ScoreActivity.class); 
        i.putExtra("myScore", score); 
        startActivity(i); 
        finish(); 
       } 
     } 

    }.start(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_arcade, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    // Respond to the action bar's Up/Home button 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     mDbHelper.close(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void load_clue(View v){ 

     //handle the click here 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

     // set title 
     alertDialogBuilder.setTitle("Clue:"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage(clue) 
      .setCancelable(false) 
      .setPositiveButton("OK",new DialogInterface.OnClickListener() { 
       private TextView txt_coins; 

       public void onClick(DialogInterface dialog,int id) { 


       } 
       }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 

    } 

}

代碼ScoresActivity:

public class ScoreActivity extends Activity { 

final TestAdapter mDbHelper = new TestAdapter(this); 
private TextView lbl_score; 
int score; 

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

    Bundle extras = getIntent().getExtras(); 
    score = extras.getInt("myScore"); 

    lbl_score = (TextView) findViewById(R.id.lbl_score); 
    final EditText txt_name = (EditText) findViewById(R.id.txt_name_score); 
    final Button btn_save = (Button) findViewById(R.id.btn_save); 

    lbl_score.setText("Your score is: "+score); 

    mDbHelper.createDatabase();  
    mDbHelper.open(); 

    btn_save.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      if(isEmpty(txt_name)){ 
       if(isEmpty(txt_name)){ 
        Toast.makeText(getApplicationContext(), "Name must not empty. ", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 



       mDbHelper.insertScore(txt_name.getText().toString(), score); 
       mDbHelper.close(); 

       Intent myIntent = new Intent(v.getContext(), ScoresActivity.class); 
       startActivityForResult(myIntent, 0); 

      } 
     } 
    }); 
} 


private boolean isEmpty(EditText etText) { 
    return etText.getText().toString().trim().length() == 0; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_arcade, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    // Respond to the action bar's Up/Home button 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     mDbHelper.close(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

+0

什麼是你正在倒計時結束後面臨 – Gaurav

+0

問題,在循環 – nards

+0

ScoreActivity負載可以編輯您的文章和觀點是什麼代碼屬於ScoreActivity和ArcadeActivity – Gaurav

回答

0

您是否啓動計時器,但不會停止它。一旦您的計時器啓動,您需要取消倒計時。裏面添加onFinish else部分()

void startTimer() { 
    countDownTimer = new CountDownTimer(currentTime, 1000) { 
     // 500 means, onTick function will be called at every 500 
     // milliseconds 

     @Override 
     public void onTick(long leftTimeInMilliseconds) { 
      seconds = leftTimeInMilliseconds/1000; 
      currentTime = leftTimeInMilliseconds; 
      lbl_timer.setText(String.format("%02d", seconds/60) 
        + ":" + String.format("%02d", seconds % 60)); 

     } 

     @Override 
     public void onFinish() { 
      // this function will be called when the timecount is finished 
       if(currentTime<1717){ 
        Toast.makeText(getApplicationContext(), "TIMES UP! "+currentTime, Toast.LENGTH_SHORT).show(); 
        Intent i = new Intent(ArcadeActivity.this, ScoreActivity.class); 
        i.putExtra("myScore", score); 
        startActivity(i); 
        finish(); 
       } else{ 
        //Added else block 
        this.cancel(); 

       } 

     } 

    }.start(); 

} 

沒有測試它。但你得到了什麼問題。

相關問題