java - 每次点击后 Activity 都会自行打开

标签 java android firebase android-intent android-activity

我正在使用 firebase 制作多人游戏。在下面的 FindOpponentActivity 中。该应用程序会检查大厅是否已存在,如果不存在,则创建一个。如果已经存在,则玩家加入大厅,并且双方玩家都进入 GameActivity,然后游戏开始。现在,创建大厅的玩家(如果你愿意的话,就是主持人)遇到了这样的问题:当 GameActivity 启动时,它会在自身之上启动多次。此外,每当单击按钮(因此调用 onDataChange)时,主机都会打开另一个 GameActivty。玩家 2 没有这样做。我做错了什么?

这是我的 FindOpponentActivity:

package com.example.android.pitjesbak;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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 FindOpponentActivity extends AppCompatActivity {

    private FirebaseDatabase db;
    private DatabaseReference lobbies;
    private DatabaseReference users;
    private DatabaseReference lobby;

    private String lobbyKey;

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    TextView status, findingOpponent;

    TextView cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findopponent);


        db = FirebaseDatabase.getInstance();
        lobbies = db.getReference("lobbies");
        users = db.getReference("users");


        final Handler handler = new Handler();

        final Runnable findOpponent = new Runnable() {
            public void run() {
                OpponentExists();
            }
        };

        handler.postDelayed(findOpponent, 5000);

        cancel = findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lobbies.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.child(user.getUid()).exists()){
                            lobbies.child(user.getUid()).removeValue();
                        }else{
                            handler.removeCallbacks(findOpponent);
                        }
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });











    }






    public void OpponentExists(){

        lobbies.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if(dataSnapshot.exists()){
                    joinOpponent();
                }else{
                    createLobby();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    };

    public void joinOpponent(){
        lobbies.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot child: dataSnapshot.getChildren()){
                    if(!child.child("player2").exists()){
                        lobbyKey = child.getKey();
                        break;
                    }
                }
                if(lobbyKey == null){
                    createLobby();
                }else{
                    lobbies.child(lobbyKey).child("player2").setValue(user.getEmail());
                    Intent startGame = new Intent(getApplicationContext(), GameActivity.class);
                    startGame.putExtra("lobbyKey", lobbyKey);
                    startGame.putExtra("player", 2);
                    startActivity(startGame);
                    FindOpponentActivity.this.finish();
                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    public void createLobby(){
        lobbies.child(user.getUid()).child("dice1").setValue(0);
        lobbies.child(user.getUid()).child("dice2").setValue(0);
        lobbies.child(user.getUid()).child("dice3").setValue(0);
        lobbies.child(user.getUid()).child("lives_p1").setValue(7);
        lobbies.child(user.getUid()).child("lives_p2").setValue(7);
        lobbies.child(user.getUid()).child("turn").setValue(1);
        lobbies.child(user.getUid()).child("has_stoeft").setValue(0);
        lobbies.child(user.getUid()).child("total_score_p1").setValue(0);
        lobbies.child(user.getUid()).child("total_score_p2").setValue(0);
        lobbies.child(user.getUid()).child("current_score_p1").setValue("0 points");
        lobbies.child(user.getUid()).child("current_score_p2").setValue("0 points");
        lobbies.child(user.getUid()).child("rolls_left").setValue(3);

        lobbies.child(user.getUid()).child("player1").setValue(user.getEmail());
        lobbyKey = user.getUid();

        lobby = lobbies.child(lobbyKey);


        lobby.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.child("player2").exists()){
                    Intent startGame = new Intent(getApplicationContext(), GameActivity.class);
                    startGame.putExtra("lobbyKey", lobbyKey);
                    startGame.putExtra("player", 1);
                    startActivity(startGame);
                    FindOpponentActivity.this.finish();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    };

}

这是我的游戏 Activity :

package com.example.android.pitjesbak;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

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;

import java.util.Random;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

public class GameActivity extends AppCompatActivity {

    Button roll, stop;

    TextView dice1, dice2, dice3, stoefen, player, other, current_score_player, current_score_other, lives_player, lives_other;

    CheckBox[] checks = new CheckBox[3];

    private int[] dice = new int[3];

    int min = 1;
    int max = 6;

    private int[] dice_scores = new int[3];
    String dice_total;

    boolean sn_4, sn_5, sn_6;

    boolean has_stoeft = false;

    int rolls_left;

    int turn = 1;

    int playing;

    String popup;


    String lobbyKey;

    FirebaseDatabase db;
    DatabaseReference lobby;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        lobbyKey= getIntent().getStringExtra("lobbyKey");

        db = FirebaseDatabase.getInstance();
        lobby = db.getReference("lobbies").child(lobbyKey);

        roll = findViewById(R.id.roll);
        stop = findViewById((R.id.stop));
        roll.setEnabled(false);

        stoefen = findViewById(R.id.stoefen);
        stoefen.setVisibility(INVISIBLE);

        roll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roll();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop();
            }
        });


        dice1 = findViewById(R.id.dice1);
        dice2 = findViewById(R.id.dice2);
        dice3 = findViewById(R.id.dice3);

        checks[0] = (CheckBox) findViewById(R.id.check1);
        checks[1] = (CheckBox) findViewById(R.id.check2);
        checks[2] = (CheckBox) findViewById(R.id.check3);

        player = findViewById(R.id.player);
        current_score_player = findViewById(R.id.current_score_player);
        lives_player = findViewById(R.id.lives_player);

        other = findViewById(R.id.other);
        current_score_other = findViewById(R.id.current_score_other);
        lives_other = findViewById(R.id.lives_other);




            lobby.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
                    int playerCheck = getIntent().getIntExtra("player", 1);
                    if(playerCheck == 1) {
                        player.setText(dataSnapshot.child("player1").getValue(String.class));
                        current_score_player.setText(dataSnapshot.child("current_score_p1").getValue(String.class));
                        lives_player.setText(dataSnapshot.child("lives_p1").getValue(Long.class).toString());

                        other.setText(dataSnapshot.child("player2").getValue(String.class));
                        current_score_other.setText(dataSnapshot.child("current_score_p2").getValue(String.class));
                        lives_other.setText(dataSnapshot.child("lives_p2").getValue(Long.class).toString());
                    }else{
                        player.setText(dataSnapshot.child("player2").getValue(String.class));
                        current_score_player.setText(dataSnapshot.child("current_score_p2").getValue(String.class));
                        lives_player.setText(dataSnapshot.child("lives_p2").getValue(Long.class).toString());

                        other.setText(dataSnapshot.child("player1").getValue(String.class));
                        current_score_other.setText(dataSnapshot.child("current_score_p1").getValue(String.class));
                        lives_other.setText(dataSnapshot.child("lives_p1").getValue(Long.class).toString());
                    }

                    turn = dataSnapshot.child("turn").getValue(int.class);
                    if(turn%2 == 0){
                        playing = 2;
                    }else{
                        playing = 1;
                    }

                    if(playing == playerCheck){
                        roll.setEnabled(true);
                    }else{
                        roll.setEnabled(false);
                        stop.setEnabled(false);
                    }


                    dice1.setText(dataSnapshot.child("dice1").getValue(Long.class).toString());
                    dice2.setText(dataSnapshot.child("dice2").getValue(Long.class).toString());
                    dice3.setText(dataSnapshot.child("dice3").getValue(Long.class).toString());

                    rolls_left = dataSnapshot.child("rolls_left").getValue(int.class);


                    if(rolls_left == 3){
                        for (int i = 0; i < 3; i++) {
                            checks[i].setEnabled(false);
                        }
                        stop.setEnabled(false);
                    }
                    if (rolls_left < 3) {

                        if (rolls_left == 2) {
                            stoefen.setVisibility(VISIBLE);
                        } else {
                            stoefen.setVisibility(INVISIBLE);
                        }

                        if(rolls_left == 0){
                            roll.setEnabled(false);
                        }

                        stop.setEnabled(true);
                        for (int i = 0; i < 3; i++) {
                            checks[i].setEnabled(true);
                        }
                    }



                    if(dataSnapshot.child("round_winner").exists()) {
                        if (dataSnapshot.child("winner").exists()) {
                            Intent intent = new Intent(getApplicationContext(), FinishActivity.class);
                            intent.putExtra("lobbyKey", lobbyKey);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            GameActivity.this.finish();
                        } else {
                            lobby.child("current_score_p1").setValue("0 points");
                            lobby.child("current_score_p2").setValue("0 points");
                            lobby.child("round_winner").removeValue();
                            lobby.child("has_stoeft").setValue(false);
                            lobby.child("rolls_left").setValue(3);
                            lobby.child("turn").setValue(1);
                            lobby.child("readyup").removeValue();
                            Intent intent = new Intent(getApplicationContext(), GameActivity.class);
                            intent.putExtra("player", playerCheck);
                            intent.putExtra("lobbyKey", lobbyKey);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            GameActivity.this.finish();
                        }
                    }




                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


    }




    public void roll(){
        if (rolls_left > 0) {
            Toast.makeText(GameActivity.this, rolls_left + " rolls left", Toast.LENGTH_SHORT).show();
        } else if (rolls_left == 0) {
            Toast.makeText(GameActivity.this, "no rolls left", Toast.LENGTH_SHORT).show();
            roll.setEnabled(false);
        }


        sn_4 = false;
        sn_5 = false;
        sn_6 = false;

        // generate numbers
        for (int i = 0; i < 3; i++) {
            if (checks[i].isChecked() == false) {
                Random r = new Random();
                int random = r.nextInt(((max - min) + 1)) + min;
                dice[i] = random;
            }else{
                if(i == 0){
                    dice[0] = Integer.parseInt(dice1.getText().toString());
                }else if(i == 1){
                    dice[1] = Integer.parseInt(dice2.getText().toString());
                }else if(i == 2){
                    dice[2] = Integer.parseInt(dice3.getText().toString());
                }
            }
        }

        lobby.child("dice1").setValue(dice[0]);
        lobby.child("dice2").setValue(dice[1]);
        lobby.child("dice3").setValue(dice[2]);



        // Set scores
        for (int i = 0; i < 3; i++) {
            if (dice[i] == 4) {
                sn_4 = true;
            }

            if (dice[i] == 5) {
                sn_5 = true;
            }

            if (dice[i] == 6) {
                sn_6 = true;
            }
        }

        if (dice[0] == dice[1] && dice[1] == dice[2]) {
            if (dice[0] == 1) {
                dice_total = "3 azen";
            } else {
                dice_total = "zand";
            }
        } else if (sn_4 == true && sn_5 == true && sn_6 == true) {
            dice_total = "soixante-neuf";
        } else {
            for (int i = 0; i < 3; i++) {
                if (dice[i] == 1) {
                    dice_scores[i] = 100;
                } else if (dice[i] == 6) {
                    dice_scores[i] = 60;
                } else {
                    dice_scores[i] = dice[i];
                }
            }

            dice_total = (dice_scores[0] + dice_scores[1] + dice_scores[2]) + " points";

        }
        if(playing == 1){
            lobby.child("current_score_p1").setValue(dice_total);
        }else{
            lobby.child("current_score_p2").setValue(dice_total);
        }

        if(rolls_left > 0){
            rolls_left -= 1;
        }else{
            roll.setEnabled(false);
        }
        lobby.child("rolls_left").setValue(rolls_left);

    };

    public void stop(){
        if(stoefen.getVisibility() == VISIBLE){
            has_stoeft = true;
            lobby.child("has_stoeft").setValue(true);
        }

        if(turn == 1){
            lobby.child("turn").setValue(2);
            lobby.child("rolls_left").setValue(3);
        }else if(turn == 2){
            int winner = 0;
            int worth = 0;
            switch (current_score_player.getText().toString()) {
                case "3 azen":
                    if (current_score_other.getText().toString().equals("3 azen")) {
                        winner = 0;
                        worth = 0;
                    } else {
                        winner = 2;
                        worth = Integer.parseInt(lives_player.getText().toString());
                    }
                    break;
                case "soixante-neuf":
                    if (current_score_other.getText().toString().equals("3 azen")) {
                        winner = 1;
                        worth = Integer.parseInt(lives_other.getText().toString());
                    } else if (current_score_other.getText().toString().equals("soixante-neuf")) {
                        winner = 0;
                        worth = 0;
                    } else {
                        winner = 2;
                        worth = 3;
                    }
                    break;
                case "zand":
                    if (current_score_other.getText().toString().equals("3 azen") || current_score_other.getText().toString().equals("soixante-neuf")) {
                        winner = 1;
                        if(current_score_other.getText().toString().equals("3 azen")){
                            worth = Integer.parseInt(lives_other.getText().toString());
                        }else{
                            worth = 3;
                        }
                    } else if (current_score_other.getText().toString().equals("zand")) {
                        winner = 0;
                        worth = 0;
                    } else {
                        winner = 2;
                        worth = 2;
                    }
                    break;
                default:
                    if (current_score_other.getText().toString().equals("3 azen") || current_score_other.getText().toString().equals("soixante-neuf") || current_score_other.getText().toString().equals("zand")) {
                        winner = 1;
                        if(current_score_other.getText().toString().equals("3 azen")){
                            worth = Integer.parseInt(lives_other.getText().toString());
                        }else if(current_score_other.getText().toString().equals("soixante-neuf")){
                            worth = 3;
                        }else if(current_score_other.getText().toString().equals("zand")){
                            worth = 2;
                        }
                    } else {
                        String[] p2_parts = current_score_player.getText().toString().split(" ");
                        int score_p2 = Integer.parseInt(p2_parts[0]);
                        String[] p1_parts = current_score_other.getText().toString().split(" ");
                        int score_p1 = Integer.parseInt(p1_parts[0]);
                        if (score_p1 > score_p2) {
                            winner = 1;
                            worth = 1;
                        } else if (score_p1 < score_p2) {
                            winner = 2;
                            worth = 1;
                        } else {
                            winner = 0;
                            worth = 0;
                        }
                    }
            }



            int lives_p1 = Integer.parseInt(lives_other.getText().toString());
            int lives_p2 = Integer.parseInt(lives_player.getText().toString());

             if( winner == 1){
                lives_p1 -= worth;
                if(has_stoeft){
                    lives_p1 --;
                }
                lobby.child("lives_p1").setValue(lives_p1);
            }else if(winner == 2){
                lives_p2 -= worth;
                if(has_stoeft){
                    lives_p1 += 2;
                }
                lobby.child("lives_p2").setValue(lives_p2);
                lobby.child("lives_p1").setValue(lives_p1);
            }

            if(lives_p1 <= 0){
                lobby.child("winner").setValue(1);
            }
            if(lives_p2 <= 0){
                lobby.child("winner").setValue(2);
            }

            lobby.child("round_winner").setValue(winner);




        }
    }
}

最佳答案

您是否尝试过在收到事件后或 Activity 拆卸期间(onStop/onDestroy)删除在 FindOpponentsActivity 中添加/附加的事件监听器?我的猜测是它仍然附加并接收事件。

关于java - 每次点击后 Activity 都会自行打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555949/

相关文章:

java - 当线程尝试调用静态和非静态同步方法时会发生什么?

java - 如何获取Glide以显示带有URI的本地照片

android - Firebase 在滚动时动态加载数据

ios - Firebase - 观察文档更改 - 如何知道它们何时来自服务器(完成)?

java - 批量 API 错误 : Upsert on Standard Salesforce Object

java - Bean 实例未加载

java - 所选项目未显示在微调器 android 上

android - 获取从 CustomListView 检查的项目的名称并在下一个 Intent 中传递该项目

java - 退出时如何保存 Activity 的状态?安卓

javascript - Firebase 身份验证异步等待