How to restart AsyncTask when wifi change?

205 Views Asked by At

I have AsyncTask class called selecting-devices in my android code and when main-activity start it will execute the class but I need to start and stop when the if user change the wifi connection of the phone.how can I do this?

final selectingDevices select=new selectingDevices();

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

          select.execute();

     }
  private class selectingDevices extends AsyncTask{

       @Override
       protected Object doInBackground(Object[] params) {
          if (!isCancelled())
          {
              startBonjour();
          }

                 return null;
       }

       @Override
       protected void onCancelled(){}
   }
1

There are 1 best solutions below

2
On

You can't restart a task.You have to create a new object if you want to call it again.

Refer to this.