0

I am calling one webservice in home page in that i will send all the details in that this process is working in bacground. when i come to this home page again i want to know prvious AsyncTask is end or not

  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");
    }

but in this when i come back to this actvity if asynctask running in background then also its showing null if i create object on top its showing pending

Mikey
  • 25
  • 1
  • 6
  • 1
    You can use the `getStatus()` method: https://developer.android.com/reference/android/os/AsyncTask#getStatus(). It returns one of these: https://developer.android.com/reference/android/os/AsyncTask.Status – HedeH Jul 02 '18 at 11:44

1 Answers1

0

Based on your text I'm making the assumption you only need this task run in this activity, and dont need multiple tasks run at once. If this is the case use an asynctaskloader. The asynctask is tied to the lifecycle of the activity and you receive callbacks when it is starting, running in background, and done.

AsyncTaskLoader basic example. (Android)

Adrian Le Roy Devezin
  • 672
  • 1
  • 13
  • 41