使用线程和异步任务无法防止ANR错误

时间:2019-04-13 00:46:04

标签: java android multithreading android-asynctask android-anr-dialog

几天来我遇到了一个大问题。实际上,在我的活动中,我调用一个NTP服务器,该服务器使用异步任务执行,并且执行多个数据库操作(我正在使用Firebase数据库),并且每当此活动打开时,都会出现ANR错误。我读到,对于长任务,我们应该使用辅助线程,因此我将数据库操作放在线程中,即使如此,我仍然会收到ANR错误。您还有什么建议? (即使我收到ANR错误,NTP服务器也会返回正确的结果,并且每次都完成数据库中的所有操作。)

public class Start extends AppCompatActivity {
//declaring variables
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);


    new Thread(new Runnable() {
        public void run(){
            finishQuiz();
        }
    }).start();

    final DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();

    myRef.addValueEventListener(new ValueEventListener() {
        //in this part, I call getNetworkTime, and under specific conditions I set an intent AlarmManager

    }

这是我在线程内调用的函数finishQuiz():

 public void finishQuiz() {

    // DEFINING THE VARIABLES NEEDED

    myRef.addListenerForSingleValueEvent( new ValueEventListener() {
        String dateD, hourD, userID, nom, prenom;
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Date d = new Date();
            timee = d.getTime();
            try {
                timee = new Start.getNetworkTime().execute().get();
            } catch (Exception e) {
                timee = d.getTime();
            }


            String dateD = dataSnapshot.child("quiz").child("dateD").getValue(String.class);
            String hourD = dataSnapshot.child("quiz").child("heureD").getValue(String.class);
            if (!(dateD.equals("")) && !(hourD.equals(""))) {

                long nbQ = dataSnapshot.child("quiz").child("questions").getChildrenCount();
                int dureeQ = Integer.valueOf(dataSnapshot.child("quiz").child("duree").getValue().toString());
                String[] D = dateD.split("-", 3);
                String[] H = hourD.split(":", 3);
                Calendar calDebut = Calendar.getInstance();
                calDebut.set(Integer.valueOf(D[2]), Integer.valueOf(D[1]) - 1, Integer.valueOf(D[0]), Integer.valueOf(H[0]), Integer.valueOf(H[1]), Integer.valueOf(H[2]));
                Date dateDebutQ = calDebut.getTime();
                long timeQFinInMillis = dateDebutQ.getTime() + (nbQ + 1) * dureeQ * 1000;

                if (timee > timeQFinInMillis) {



                    try {
                        final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                        Calendar caldateQ = Calendar.getInstance();
                        String[] dateDT = dateD.split("-", 3);
                        String[] hourDT = hourD.split(":", 3);
                        caldateQ.set(Integer.valueOf(dateDT[2]), Integer.valueOf(dateDT[1]) - 1, Integer.valueOf(dateDT[0]), Integer.valueOf(hourDT[0]), Integer.valueOf(hourDT[1]), Integer.valueOf(hourDT[2]));
                        long c = caldateQ.getTimeInMillis();
                        List<Date> listPast = new ArrayList<>();
                        List<Date> listFuture = new ArrayList<>();
                        for (DataSnapshot ds : dataSnapshot.child("next quizs").getChildren()) {
                            String dateNext = ds.getKey();
                            try {

                                Date dateNx = formatter.parse(dateNext);
                                Date dateQQQ = caldateQ.getTime();
                                Date dateQQ = formatter.parse(formatter.format(dateQQQ));
                                long timeNx = dateNx.getTime();
                                if (timeNx < timee) {
                                    listPast.add(dateNx);

                                }
                                else
                                {
                                    listFuture.add(dateNx);
                                }
                            } catch (Exception e) {

                            }
                        }

                        for (int i = 0; i < listPast.size(); i++) {
                            myNextQuiz.child(formatter.format(listPast.get(i))).removeValue();
                        }

                        if (listFuture.size()==0) {
                            myQuiz.child("dateD").setValue("");
                            myQuiz.child("heureD").setValue("");
                            myQuiz.child("duree").setValue(10);
                            myQuiz.child("questions").setValue("This will be removed anyways");
                        } else 
//I get ANR error in this case
{



                                Date minuD = listFuture.get(0);
                                for (int k = 1; k < listFuture.size(); k++) {
                                    if ((listFuture.get(k).compareTo(minuD) < 0)) {
                                        minuD = listFuture.get(k);
                                    }
                                }
                                    String date = formatter.format(minuD);
                                    String[] DH = date.split(" ", 2);
                                    myQuiz.child("dateD").setValue(DH[0]);
                                    myQuiz.child("heureD").setValue(DH[1] + ":00");
                                    String ddd = dataSnapshot.child("next quizs").child(date).child("duree").getValue().toString();
                                    myQuiz.child("duree").setValue(ddd);
                                    myQuiz.child("questions").removeValue();
                                    int j = 1;
                                   for (DataSnapshot ds : dataSnapshot.child("next quizs").child(date).child("questions").getChildren()) {
                                       String quest = ds.child("question").getValue(String.class);
                                       String sol = ds.child("sol").getValue(String.class);
                                       String answA = ds.child("A").getValue(String.class);
                                       String answB = ds.child("B").getValue(String.class);
                                       String answC = ds.child("C").getValue(String.class);
                                       String answD = ds.child("D").getValue(String.class);
                                       myQuiz.child("questions").child("" + j).child("question").setValue(quest);
                                       myQuiz.child("questions").child("" + j).child("sol").setValue(sol);
                                       myQuiz.child("questions").child("" + j).child("A").setValue(answA);
                                       myQuiz.child("questions").child("" + j).child("B").setValue(answB);
                                       myQuiz.child("questions").child("" + j).child("C").setValue(answC);
                                       myQuiz.child("questions").child("" + j).child("D").setValue(answD);
                                       j++;
                                   }
                            }

                        myPreQuiz.child(dateD + " " + hourD).child("dateD").setValue(dateD);
                        myPreQuiz.child(dateD + " " + hourD).child("heureD").setValue(hourD);
                        for (DataSnapshot ds : dataSnapshot.child("current quiz").child("number").getChildren()) {
                            userID = ds.getKey();
                            nom = dataSnapshot.child("utilisateurs").child(userID).child("Nom").getValue(String.class);
                            prenom = dataSnapshot.child("utilisateurs").child(userID).child("Prenom").getValue(String.class);
                            myPreQuiz.child(dateD + " " + hourD).child("gagnants").child(userID).setValue(nom + " " + prenom);
                        }
                        myCurrQuiz.child("currentQ").setValue("0");
                        myCurrQuiz.child("number").removeValue();

                    } catch (Exception e) {
                       //show Exception

                  } }}}

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

        }
    });}

这是我的异步任务:

private class getNetworkTime extends AsyncTask<Void, Void, Long> {

    @Override
    protected Long doInBackground(Void... params) {
        String TIME_SERVER = "2.android.pool.ntp.org";
        try{
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();   //server time

        return returnTime;
    } catch (Exception e) {
        Date d = new Date();
        long l=d.getTime();
        return l;
    }
    }


    protected void onPostExecute(Long result) {

    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

如果有帮助,在我使用NTP服务器之前,我的代码曾经可以正常工作,并且我在没有AlarmManager部件的情况下进行了测试,并且也可以正常工作,还可以在函数finishQuiz()中进行了测试,如我在a中提到的那样。注释,只有该特定情况才会产生ANR错误(这恰好是我的应用程序中最常见的情况),我的意思是,无需输入该代码块,就不会出现ANR错误。

1 个答案:

答案 0 :(得分:0)

即使您开始在后台线程上执行finishQuiz方法,firebase回调也会在主(UI)线程上回调。这可能会导致您的ANR。

尝试在后台线程的onDataChange()回调中执行代码。

new Thread(new Runnable() {
    public void run(){

          // PUT YOUR CODE FROM onDataChange() HERE, or use AsyncTask


    }
}).start();

查看此内容:Firebase Android: onDataChange() event always executed in Main UI Thread?