我最近开始在Android中使用Firebase。
现在,当我尝试从Firebase实时数据库中检索数据时,检索过程将在一段时间后执行,直到此过程下面的代码首先执行。
因此,我无法将值存储在变量中,并且空变量将传递给下一个活动。
public class deffi_key extends AppCompatActivity {
EditText a_key,b_key,p_k1,p_k2;
AlertDialog.Builder builder;
long aliceid;
FirebaseAuth mAuth;
DatabaseReference reference;
String key_val="",alice_val="",bob_val="";
long bobid,keyid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deffi_key);
p_k1 = findViewById(R.id.gkey_p);
p_k2 = findViewById(R.id.gkey_g);
a_key = findViewById(R.id.p_k1);
b_key = findViewById(R.id.p_k2);
mAuth = FirebaseAuth.getInstance();
reference = FirebaseDatabase.getInstance().getReference("Diffi hell man").child(mAuth.getCurrentUser().getUid());
reference.child("Alice").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
aliceid = dataSnapshot.getChildrenCount();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
if (mAuth.getCurrentUser()==null)
{
startActivity(new Intent(getApplicationContext(),login.class));
finish();
}
builder = new AlertDialog.Builder(this);
}
public void alice(String alice_data)
{
alice_val += alice_data;
}
public void bob(String bob_data)
{
bob_val += bob_data;
}
public void key(String key_data)
{
key_val += key_data;
}
public int alice_initialize()
{
long r = aliceid;
for (long i=1;i<=r;i++)
{
reference = FirebaseDatabase.getInstance().getReference("Diffi hell man").child(mAuth.getCurrentUser().getUid()).child("Alice").child(String.valueOf(i));
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
String alice_pt = dataSnapshot.child("alice_pt").getValue().toString();
String alice_send = dataSnapshot.child("alice_sent").getValue().toString();
String alice_receive = dataSnapshot.child("alice_receive").getValue().toString();
String time = dataSnapshot.child("time").getValue().toString();
String date = dataSnapshot.child("date").getValue().toString();
String alice_data = alice_pt+ "," +alice_send+ "," +alice_receive+ "," +date+ "," +time+"@";
alice(alice_data);
}
catch (Exception e)
{
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return 1;
}
public int bob_initialize() {
long r = bobid;
for (long i = 1; i <= r; i++) {
reference = FirebaseDatabase.getInstance().getReference("Diffi hell man").child(mAuth.getCurrentUser().getUid()).child("bob").child(String.valueOf(i));
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
String bob_pt = dataSnapshot.child("bob_pt").getValue().toString();
String bob_send = dataSnapshot.child("bob_sent").getValue().toString();
String bob_receive = dataSnapshot.child("bob_receive").getValue().toString();
String time = dataSnapshot.child("time").getValue().toString();
String date = dataSnapshot.child("date").getValue().toString();
String bob_data = bob_pt + "," + bob_send + "," + bob_receive + "," + date + "," + time + "@";
bob(bob_data);
} catch (Exception e) {
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return 1;
}
public int key_initialize() {
long r = keyid;
for (long i = 1; i <= r; i++) {
reference = FirebaseDatabase.getInstance().getReference("Diffi hell man").child(mAuth.getCurrentUser().getUid()).child("Key").child(String.valueOf(i));
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
String key_pt = dataSnapshot.child("key_pt").getValue().toString();
String key_send = dataSnapshot.child("key_sent").getValue().toString();
String key_receive = dataSnapshot.child("key_receive").getValue().toString();
String time = dataSnapshot.child("time").getValue().toString();
String date = dataSnapshot.child("date").getValue().toString();
String key_data = key_pt + "," + key_send + "," + key_receive + "," + date + "," + time + "@";
key(key_data);
} catch (Exception e) {
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return 1;
}
public long key_gen(long p,long g,long key_a,long key_b)
{
long x,y;
x = (int)Math.pow(g,key_a) % p;
y = (int)Math.pow(g,key_b) % p;
key_a = (int)Math.pow(y,key_a) % p;
key_b = (int)Math.pow(x,key_b) % p;
System.out.println(key_a+" "+key_b);
return key_a;
}
public void clear(View view) {
p_k1.setText(null);
p_k2.setText(null);
a_key.setText(null);
b_key.setText(null);
}
public void submit(View view) {
String key_a1 =a_key.getText().toString(), key_b1 = b_key.getText().toString(), p1 = p_k1.getText().toString(), g1 = p_k2.getText().toString();
if (key_a1.equals(""))
a_key.setError("Field is empty!");
if (key_b1.equals(""))
b_key.setError("Field is empty!");
if (p1.equals(""))
p_k1.setError("Field is empty!");
if (g1.equals(""))
p_k2.setError("Field is empty!");
else{
long key_a = Long.parseLong(key_a1), key_b = Long.parseLong(key_b1), p = Long.parseLong(p1), g = Long.parseLong(g1);
Intent intent = new Intent(this, diffie_display.class);
long secret_key = key_gen(p,g,key_a,key_b);
intent.putExtra("secret_key",secret_key);
intent.putExtra("p",p);
intent.putExtra("g",g);
intent.putExtra("alice",key_a);
intent.putExtra("bob",key_b);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.removedb,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.logout:
builder.setMessage("Are you sure that you want to logout ?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mAuth.signOut();
startActivity(new Intent(getApplicationContext(),login.class));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setTitle("logout.");
alertDialog.show();
break;
case R.id.viewhis:
alice_initialize();
bob_initialize();
key_initialize();
Intent intent = new Intent(this, diffie_history.class);
intent.putExtra("alice",aliceid);
intent.putExtra("a_txt",alice_val);
intent.putExtra("b_txt",bob_val);
intent.putExtra("k_txt",key_val);
System.out.println("sdsdd09"+alice_val);
startActivity(intent);
break;
case R.id.clrhistory:
builder.setMessage("Do you want Clear Your History ?").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
reference.removeValue();
aliceid=0;
Toast.makeText(getApplicationContext(),"Your history has been Deleted!!.", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.setTitle("Clear History");
alert.show();
break;
}
return super.onOptionsItemSelected(item);
}
}