如何知道AsyncTask是否在后台工作?

时间:2018-07-02 11:41:51

标签: android json android-asynctask background-process android-background

我要在主页中调用一个Web服务,因为我将发送所有详细信息,因为此过程正在免费中进行。当我再次进入此主页时,我想知道以前的AsyncTask是否结束

  what iam doing exactly here



           public void callAsynchronousTask() {
    //http://stackoverflow.com/questions/6531950/how-to-execute-async-task-repeatedly-after-fixed-time-intervals
    final Handler handler = new Handler();
    Timer timer = new Timer();

        doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {

                            if (vist != null && saves != null && prof != null && semail != null) {
                                if (vist.getStatus() != AsyncTask.Status.RUNNING) {

                                    if (saves.getStatus() == AsyncTask.Status.PENDING || saves.getStatus() == AsyncTask.Status.FINISHED && saves.getStatus() != AsyncTask.Status.RUNNING)
                                    {

                                        if (prof.getStatus() == AsyncTask.Status.PENDING || prof.getStatus() == AsyncTask.Status.FINISHED && prof.getStatus() != AsyncTask.Status.RUNNING)
                                        {


                                            if (semail.getStatus() == AsyncTask.Status.PENDING || semail.getStatus() == AsyncTask.Status.FINISHED && semail.getStatus() != AsyncTask.Status.RUNNING)
                                            {

                                                checkandsave();
                                            }


                                        }
                                    }
                                }
                            } else {
                                checkandsave();
                            }


       public void checkandsave(){

                    ConnectionDetector cd = new ConnectionDetector(Home_page.this);
    Boolean isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {




        DatabaseToadd da = new DatabaseToadd(Home_page.this);
        mu = new ArrayList<>();
        mu = da.getAllUser();
        if (mu.size() > 0) {


            vist = new Getvisiterid();

         saves=new SaveVisitor();
            semail=new sendemail();
            prof=new saveprofpic();
            vist.executeOnExecutor(THREAD_POOL_EXECUTOR);
        }









    }
    else {
        System.out.println("null db");
    }

但是在这种情况下,如果asynctask在后台运行,那么当我回到该活动时,如果我在其显示待处理的顶部创建对象,则显示为空

1 个答案:

答案 0 :(得分:0)

根据您的文字,我假设您只需要在此活动中运行此任务,而无需一次运行多个任务。如果是这种情况,请使用asynctaskloader。 asynctask与活动的生命周期相关联,当活动开始,在后台运行并完成时,您会收到回调。

AsyncTaskLoader basic example. (Android)