Rsl
WM Aracı
Kullanıcı
-
Üyelik
03.05.2017
-
Yaş/Cinsiyet
25 / E
-
Meslek
Android Junior Developer
-
Konum
İstanbul Avrupa
-
Ad Soyad
R** G**
-
Mesajlar
57
-
Beğeniler
3 / 16
-
Ticaret
1, (%100)
Amatör olarak mobil bilgi yarışması yazdım ve bitirme aşamasına geldim, fakat son anda ilerleme yapamadığım için uygulamada kitlendim. Oyun başladığı esnada kullanıcıya 11 saniye veriyorum ve geri saymaya başlıyor burada sorun yok fakat 11 saniyede kaç tane soru cevaplayabilirse gibi bir işleyişe sahip. Ben bunu her 11 saniyede 1 soru gelecek şekilde ayarlamak istiyorum ve yapamıyorum Bu konu hakkında yardım edebilirmisiniz?
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.Model.Question;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class OynaActivity extends AppCompatActivity {
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
FirebaseDatabase db;
DatabaseReference ref;
CountDownTimer timer;
TextView textViewSure, t1_question;
String oyunAdi;
TableLayout tableLayoutSorular;
Button btnBasla;
ValueEventListener durumListener;
int dogru;
Button b1, b2, b3, b4;
int total = 1;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_oyna);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
textViewSure = findViewById(R.id.textView4);
tableLayoutSorular = findViewById(R.id.tableLayoutSorular);
tableLayoutSorular.setVisibility(View.INVISIBLE);
btnBasla = findViewById(R.id.btnBasla);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
t1_question = (TextView) findViewById(R.id.questionsTxt);
loadAds();
updateQuestion();
//Firebase database nesneleri oluşturuluyor
firebaseAuth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
ref = db.getReference();
//Giriş yapan kullanıcı bilgileri (email) alınıyor.
firebaseUser = firebaseAuth.getCurrentUser();
setTitle(firebaseUser.getEmail());
//Kullanıcı adı olarak email'in @ işaretinden önceki kısmı alınıyor.
final String username = firebaseUser.getEmail().split("@")[0];
//Intent ile önceki activityden gelen oyunadı alınıyor.Ör. Ahmet
oyunAdi = getIntent().getStringExtra("oyunAdi");
//Eğer bu kullanıcı oyun kurucu ise başla butonunu aktifleştir. Değilse durum değerini dinle(listener ile)
if (oyunAdi.equals(username)) {
btnBasla.setEnabled(true);
} else {
btnBasla.setEnabled(false);
btnBasla.setText("Lütfen bekleyin...");
//durum değerini takip et, durum=başla ise timer'ı başlat
durumListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String durum = dataSnapshot.getValue(String.class);
if (durum.equals("başladı")) {
timer.start();
//Soruları göster
tableLayoutSorular.setVisibility(View.VISIBLE);
btnBasla.setVisibility(View.INVISIBLE);
Toast.makeText(OynaActivity.this, "Oyun Başladı", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
ref.child("oyunlar").child(oyunAdi).child("durum").addValueEventListener(durumListener);
}
timer = new CountDownTimer(11000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
textViewSure.setText("Kalan süre : " + String.valueOf(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
Toast.makeText(OynaActivity.this, "Süre doldu", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(OynaActivity.this, SonucActivity.class);
intent.putExtra("oyunAdi", oyunAdi);
startActivity(intent);
finish();
}
};
}
public void btnOyunaBasla(View v) {
ref.child("oyunlar").child(oyunAdi).child("durum").setValue("başladı");
timer.start();
btnBasla.setVisibility(View.INVISIBLE);
//Soruları göster
tableLayoutSorular.setVisibility(View.VISIBLE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//Activtiy kapanırken (Süre bittiğinde) listener'ı sil
if (durumListener != null) {
ref.child("oyunlar").child(oyunAdi).child("durum").removeEventListener(durumListener);
}
}
private void updateQuestion() {
if (total > 100) {
} else {
reference = FirebaseDatabase.getInstance().getReference().child("Questions").child(String.valueOf(total));
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final Question question = dataSnapshot.getValue(Question.class);
t1_question.setText(question.getQuestion());
b1.setText(question.getOption1());
b2.setText(question.getOption2());
b3.setText(question.getOption3());
b4.setText(question.getOption4());
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
total++;
if (b1.getText().toString().equals(question.getAnswer())) {
b1.setBackgroundColor(Color.parseColor("#4CAF50"));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dogru++;
b1.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
} else {
b1.setBackgroundColor(Color.parseColor("#FF0000"));
if (b2.getText().toString().equals(question.getAnswer())) {
b2.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b3.getText().toString().equals(question.getAnswer())) {
b3.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b4.getText().toString().equals(question.getAnswer())) {
b4.setBackgroundColor(Color.parseColor("#4CAF50"));
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundColor(Color.parseColor("#79899d"));
b2.setBackgroundColor(Color.parseColor("#79899d"));
b3.setBackgroundColor(Color.parseColor("#79899d"));
b4.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
total++;
if (b2.getText().toString().equals(question.getAnswer())) {
b2.setBackgroundColor(Color.parseColor("#4CAF50"));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dogru++;
b2.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
} else {
b2.setBackgroundColor(Color.parseColor("#FF0000"));
if (b1.getText().toString().equals(question.getAnswer())) {
b1.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b3.getText().toString().equals(question.getAnswer())) {
b3.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b4.getText().toString().equals(question.getAnswer())) {
b4.setBackgroundColor(Color.parseColor("#4CAF50"));
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundColor(Color.parseColor("#79899d"));
b2.setBackgroundColor(Color.parseColor("#79899d"));
b3.setBackgroundColor(Color.parseColor("#79899d"));
b4.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
total++;
if (b3.getText().toString().equals(question.getAnswer())) {
b3.setBackgroundColor(Color.parseColor("#4CAF50"));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dogru++;
b3.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
} else {
b3.setBackgroundColor(Color.parseColor("#FF0000"));
if (b1.getText().toString().equals(question.getAnswer())) {
b1.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b2.getText().toString().equals(question.getAnswer())) {
b2.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b4.getText().toString().equals(question.getAnswer())) {
b4.setBackgroundColor(Color.parseColor("#4CAF50"));
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundColor(Color.parseColor("#79899d"));
b2.setBackgroundColor(Color.parseColor("#79899d"));
b3.setBackgroundColor(Color.parseColor("#79899d"));
b4.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
total++;
if (b4.getText().toString().equals(question.getAnswer())) {
b4.setBackgroundColor(Color.parseColor("#4CAF50"));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dogru++;
b4.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
}
}, 1500);
} else {
b4.setBackgroundColor(Color.parseColor("#FF0000"));
if (b1.getText().toString().equals(question.getAnswer())) {
b1.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b2.getText().toString().equals(question.getAnswer())) {
b2.setBackgroundColor(Color.parseColor("#4CAF50"));
} else if (b3.getText().toString().equals(question.getAnswer())) {
b3.setBackgroundColor(Color.parseColor("#4CAF50"));
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundColor(Color.parseColor("#79899d"));
b2.setBackgroundColor(Color.parseColor("#79899d"));
b3.setBackgroundColor(Color.parseColor("#79899d"));
b4.setBackgroundColor(Color.parseColor("#79899d"));
updateQuestion();
total++;
}
}, 1500);
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
private void loadAds() {
//Reklam kodları
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void onBackPressed() {
//Geri basıldığında gidiecek activity
super.onBackPressed();
Intent intent = new Intent(OynaActivity.this, AnaMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
}
}