应用程序在主线程上做了太多工作(Firebase和OnLongClickListener)

时间:2018-04-03 12:27:49

标签: java android multithreading firebase-realtime-database

我正在尝试让我的应用程序向firebase发送信息,首先它允许它将信息发送到firebase,但是第二次它显示了一条消息 “跳过n#frames!应用程序可能在其主线程上做了太多工作。”和“后台并发复制GC释放185001(8MB)AllocSpace对象,0(0B)LOS对象,31%免费,12MB / 18MB ......”。

这个主要功能是发送有关游戏的信息,如果它的按钮已经长时间点击(OnLongClickListener)。

此函数的java文件是:

HomePageActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomePageActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private Firebase FavDBRef;
    private FirebaseUser firebaseUser;
    private Button LeagueButton, OWButton, CSGOButton, DOTAButton;

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

    }

    public void setupUIViews(){
        LeagueButton = findViewById(R.id.leagueButton);
        OWButton = findViewById(R.id.owButton);
        CSGOButton = findViewById(R.id.csgoButton);
        DOTAButton = findViewById(R.id.dotaButton);
    }

    public void onStart() {
        super.onStart();
        FirebaseUser currentUser = firebaseAuth.getCurrentUser();
    }

    public void gameButtons(){

        Firebase.setAndroidContext(this);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseUser = firebaseAuth.getCurrentUser();

        FavDBRef = new Firebase("https://mwt-esportsinfo-userfav.firebaseio.com/" + firebaseUser.getUid()).child("FavEsport");

        LeagueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HomePageActivity.this, LeagueOfLegends.class));
            }
        });

        LeagueButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                FavDBRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String FavEsport = "League of Legends";
                        String FavEsportDev = "Riot Games";
                        String FavEsportRel = "27 October 2009";
                        String FavEsportGenre = "Multiplayer Online Battle Arena";
                        String FavEsportImg = "https://vignette.wikia.nocookie.net/leagueoflegends/images/8/86/League_of_legends_logo_transparent.png/revision/latest/scale-to-width-down/640?cb=20131023175853";

                        UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

                        FavDBRef.setValue(userFavESport);

                        Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });

                return false;
            }
        });

        OWButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                FavDBRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String FavEsport = "Overwatch";
                        String FavEsportDev = "Blizzard Entertainment";
                        String FavEsportRel = "24 May 2016";
                        String FavEsportGenre = "First-Person Shooter";
                        String FavEsportImg = "https://vignette.wikia.nocookie.net/dbxfanon/images/8/84/Overwatch_logo_by_feeerieke-da4xuzp.png/revision/latest?cb=20170316184119";

                        UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

                        FavDBRef.setValue(userFavESport);

                        Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });

                return false;
            }
        });

        CSGOButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                FavDBRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String FavEsport = "Counter-Strike: Global Offensive";
                        String FavEsportDev = "Valve Corporation";
                        String FavEsportRel = "21 August 2012";
                        String FavEsportGenre = "First-Person Shooter";
                        String FavEsportImg = "https://vignette.wikia.nocookie.net/logopedia/images/c/c8/CSGO.png/revision/latest?cb=20150828062634";

                        UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

                        FavDBRef.setValue(userFavESport);

                        Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });

                return false;
            }
        });

        DOTAButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                FavDBRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String FavEsport = "DOTA 2";
                        String FavEsportDev = "Valve Corporation";
                        String FavEsportRel = "9 July 2013";
                        String FavEsportGenre = "Multiplayer Online Battle Arena";
                        String FavEsportImg = "http://www.userlogos.org/files/logos/Kravlenissen/Dota-2%284x3%291-1.png";

                        UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

                        FavDBRef.setValue(userFavESport);

                        Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });

                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.profileMenu){
            startActivity(new Intent(HomePageActivity.this, UserProfileActivity.class));
        }
        if (item.getItemId() == R.id.logoutMenu){
            Logout();
        }
        else{
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    private void Logout(){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(HomePageActivity.this, LoginActivity.class));
    }
}

并检索这些信息。

UserFavESport.java

public class UserFavESport {
    String favEsports, favEsportsDev, favEsportsRel, favEsportsGenre, favEsportsImg ;

    public UserFavESport(){

    }

    public UserFavESport(String favEsports, String favEsportsDev, String favEsportsRel, String favEsportsGenre, String favEsportsImg) {
        this.favEsports = favEsports;
        this.favEsportsDev = favEsportsDev;
        this.favEsportsRel = favEsportsRel;
        this.favEsportsGenre = favEsportsGenre;
        this.favEsportsImg = favEsportsImg;
    }

    public String getFavEsports() {
        return favEsports;
    }

    public void setFavEsports(String favEsports) {
        this.favEsports = favEsports;
    }

    public String getFavEsportsDev() {
        return favEsportsDev;
    }

    public void setFavEsportsDev(String favEsportsDev) {
        this.favEsportsDev = favEsportsDev;
    }

    public String getFavEsportsRel() {
        return favEsportsRel;
    }

    public void setFavEsportsRel(String favEsportsRel) {
        this.favEsportsRel = favEsportsRel;
    }

    public String getFavEsportsGenre() {
        return favEsportsGenre;
    }

    public void setFavEsportsGenre(String favEsportsGenre) {
        this.favEsportsGenre = favEsportsGenre;
    }

    public String getFavEsportsImg() {
        return favEsportsImg;
    }

    public void setFavEsportsImg(String favEsportsImg) {
        this.favEsportsImg = favEsportsImg;
    }
}

对此的任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:2)

错误"应用程序可能在其主线程上做了太多工作"告诉你在主线程上做了很长时间的操作。我们应该总是避免在主线程上执行长操作,而是在后台线程上执行。除了在按钮的长按单元监听器中显示toast之外,所有代码都应该是新线程而不是主线程。只需在click侦听器方法中创建一个新线程,并将此工作委托给新线程。

如果要将结果传回UI线程,可以使用Thread和Runnable处理主线程数据或使用Async任务。