英文:
Can´t finish Activity
问题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_cerca);
fAuth = FirebaseAuth.getInstance();
entr = findViewById(R.id.AddButton);
mDatabase = FirebaseDatabase.getInstance().getReference();
sDatabase = FirebaseDatabase.getInstance().getReference();
aDatabase = FirebaseDatabase.getInstance().getReference();
MAC =findViewById(R.id.macCerca);
NOM = findViewById(R.id.momCerca);
configured = false;
getUserProfile();
getNotificationId();
entr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MacCerca = MAC.getText().toString().toUpperCase();
NombreCerca = NOM.getText().toString();
mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String user1 = (String) dataSnapshot.child("1").getValue();
String user2 = (String) dataSnapshot.child("2").getValue();
String user3 = (String) dataSnapshot.child("3").getValue();
String user4 = (String) dataSnapshot.child("4").getValue();
if(user1 == null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("1").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user2 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("2").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user3 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("3").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user4 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("4").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else{
Toast.makeText(AddCerca.this, "Límite de usuarios registrados excedidos", Toast.LENGTH_SHORT).show();
AddCerca.this.finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
This is the code of the activity that sends the information.
英文:
I have tried className.this.finish and finishActivity() to invoke the finishing of my activity and I have initialised the activity with the following methods - startActivityForResult() or finishAndRemoveTask()
The propose of this activity is to register a device this is done when under Devices/(MAC address of the device)/Users/ it save in fire base up to 4 different notification tokens of 4 users different. So what I'm trying to do but it falling is to check if the number 1 have info, check 2 if have info, check 3 and if this have info, check the 4 if all have info the app show a message saying "you have reached the maximum number of users".But if one number available it suppose to save in that number and only in that number the notification token.
What it is happening is that when I click the button the loop never end. it send the same notification token for the four users show the mensage "you have reached the maximum number of users" and then it come back to the activity before this one but it still executing the registration code. I know this because if I deleted an user in firebase it immediately resend the info and you can see the message "you have reached the maximum number of users" again
this is the code of the activity that send the info:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_cerca);
fAuth = FirebaseAuth.getInstance();
entr = findViewById(R.id.AddButton);
mDatabase = FirebaseDatabase.getInstance().getReference();
sDatabase = FirebaseDatabase.getInstance().getReference();
aDatabase = FirebaseDatabase.getInstance().getReference();
MAC =findViewById(R.id.macCerca);
NOM = findViewById(R.id.momCerca);
configured = false;
getUserProfile();
getNotificationId();
entr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MacCerca = MAC.getText().toString().toUpperCase();
NombreCerca = NOM.getText().toString();
//mDatabase.child("Devices").child(MacCerca).child("Id").setValue("AD:23");
mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String user1 = (String) dataSnapshot.child("1").getValue();
String user2 = (String) dataSnapshot.child("2").getValue();
String user3 = (String) dataSnapshot.child("3").getValue();
String user4 = (String) dataSnapshot.child("4").getValue();
if(user1 == null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("1").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user2 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("2").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user3 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("3").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user4 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("4").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else{
Toast.makeText(AddCerca.this, "Límite de usuarios registrados exedidos", Toast.LENGTH_SHORT).show();
AddCerca.this.finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
and I create this activity like this:
startActivity(new Intent(CercaElectrica.this, AddCerca.class));
Firebase look like this:
When it suposed to be only in the user 1, or if user 1 have some data it have to update user 2 and the same with 3 and 4
答案1
得分: 0
我发现了错误。改为:
mDatabase.child("Devices").child(MacCerca).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
英文:
I found the error. insted of:
mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
change for:
mDatabase.child("Devices").child(MacCerca).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论