SUMMARY
I have a game game. User clicks image:


The user has 5 seconds to click and select the correct one. After 5 seconds, the correct image will be displayed differently to attract attention, and again after 5 seconds the user will see the next level.
the user can click without waiting for 5 seconds, so when you click, you should go to the next level.
I create viewing and viewing images dynamically. Representations of images are enclosed in a local sqlite database as bytes.
My method creates the oncreate method inside. And I use a while or while loop. Each iteration is a game level. Each level has different image images and different image values.
Delay 5 + 5 seconds I had to use handlers, threads, countdowntimers. Each of them caused problems.
When using handlers, when the user pressed for up to 5 seconds, I could not end the current session.
I do not know if this is correct.
You can see my codes below:
LONG EXPLANATIONS AND CODES
Timer:
nCreate() { for (int i = 0; i < 2; i++) { handler.postDelayed(new Runnable() { public void run() { imageAnswer.setImageBitmap(bmp); imageAnswer.setId(R.id.imgAnswer); //adding image to screen finalRLayout.removeAllViews(); finalRLayout.addView(imageAnswer, rLayParams); //starting timer, if no answer in 5 seconds, run again timer startTimer(); //clicklisterner imageAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //if clicks true, cancel timer and go to next iteration of for loop if (view == findViewById(R.id.imgAnswer)) { Log.d(TAG, "clicked"); //PUT IN CODE HERE TO GET NEXT IMAGE cancelTimer(); } else { //if not true, run again timer for last chance Log.d(TAG, "nonclick"); cancelTimer(); startTimer(); } } }); } }, 2000 * i); } //for end } //oncreate end //start timer function void startTimer() { cTimer = new CountDownTimer(5000, 1000) { public void onTick(long millisUntilFinished) { Log.d(TAG, "ontick"); } public void onFinish() { Log.d(TAG, "timer onfinish"); } }; cTimer.start(); } //cancel timer void cancelTimer() { Log.d(TAG, "canceltimer"); if (cTimer != null) cTimer.cancel(); }
3 different threads, but not working. one of them made the screen go black. the other two did not block the loop.
runnable version for ( int i = 0; i< 20 ; i++) { Log.d(TAG, "for i2="+ i); final int finalI = i; final RelativeLayout finalRLayout = rLayout; final Runnable r=new Runnable() { @Override public void run() { Log.d(TAG, "for finali2="+ finalI); TrainingObject trainingObject = new TrainingObject(); trainingObject = trainingObjectList.get(finalI); objectCount = 2; //test icin Log.d(TAG,"testicin trainingobjectid: "+trainingObject.getTrainingobjectID()); object = dbHandler.getObjectObject(trainingObject.getTrainingobjectAnswer()); RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(140,140); rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT); imgBytes = object.getObjectImageBlob(); bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length); imageAnswer.setImageBitmap(bmp); imageAnswer.setTag(trainingObject.getTrainingobjectAnswer()); imageAnswer.setId(R.id.imgAnswer); finalRLayout.removeAllViews(); finalRLayout.addView(imageAnswer,rLayParams); } }; Log.d(TAG, "3000i2 sonrasi"); handler.postDelayed(r, 6000 * i); imageAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (view == findViewById(R.id.imgAnswer)) { Log.d(TAG, "clicked");handler.removeCallbacks(r); handler.postDelayed(r, 0); //PUT IN CODE HERE TO GET NEXT IMAGE } else{ Log.d(TAG, "nonclick"); handler.removeCallbacks(r); handler.postDelayed(r, 6000); } } }); }//for end