Paul Jose
Lurker
I am trying to create a thread in an android application to keep checking my firebase database to check if an otp value has been put in the child value of a node. But it keeps crashing as soon as I start the application. This is my code so far.
//Thread check otp
Thread t = new Thread() {
@override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@override
public void run() {
//check OTP if present on database
FirebaseDatabase.getInstance().getReference().child("Keys")
.addListenerForSingleValueEvent(new ValueEventListener() {
@override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Subjects keys = snapshot.getValue(Subjects.class);
//System.out.println(user.email);
Intent i = new Intent(getApplicationContext(), markAttendance.class);
i.putExtra("otp",keys.otpcode);
i.putExtra("subid", keys.subjectId);
finish();
startActivity(i);
}
}
@override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
//Thread check otp
Thread t = new Thread() {
@override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@override
public void run() {
//check OTP if present on database
FirebaseDatabase.getInstance().getReference().child("Keys")
.addListenerForSingleValueEvent(new ValueEventListener() {
@override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Subjects keys = snapshot.getValue(Subjects.class);
//System.out.println(user.email);
Intent i = new Intent(getApplicationContext(), markAttendance.class);
i.putExtra("otp",keys.otpcode);
i.putExtra("subid", keys.subjectId);
finish();
startActivity(i);
}
}
@override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();