0

I'm quite new in mobile development and also in JAVA language and I'm havin a big trouble now, because android 11 is comming to a lot of devices and I need to update the classes of a project to stop using AsyncTask (which is deprecated) but I quite dont understand how to update the following class to stop using AsyncTask, can anyone help me please?

obs: I'm brasilian so there are some words in portuguese :P

public class ChecarStatusTask extends AsyncTask<Context, Void, JsonObject> {
  private Context context;
  public AsyncResponse delegate;
  @Override
  protected JsonObject doInBackground(Context... params) {
      context = params[0];
      JsonObject json = new JsonObject();

      Network net = new Network(context);

      boolean deveMostrarProblemaConexao = DashboardActivity.deveMostrarProblemaConexao(context);
      boolean problemaInternet = true;

      //-- verify if connected
      json.addProperty("Rede", "NO CONNECTION");
      if (net.isNetworkAvailable()) {
          json.remove("Rede");
          json.addProperty("Rede", "OK");
      }

      //checking http with google
      json.addProperty("Internet", "NO CONNECTION");
      if(Network.checkHttpStatus("https://google.com")) {
          json.remove("Internet");
          json.addProperty("Internet", "OK");
          problemaInternet = false;
      }

      if(!deveMostrarProblemaConexao && problemaInternet) {
          return json;
      }

      return json;
  }

  @Override
  protected void onPostExecute(JsonObject result) {
      try {
          delegate.processFinish(result);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

}
  • If a class is only deprecated, but not removed entirely, you can still use it. I'd wait until your Java knowledge is better before updating something fairly complicated like AsyncTask. – markspace Feb 11 '21 at 20:51
  • @markspace but we're having some toubles when this class is executed, like, it takes several seconds to return, I was thinking that it happens because the AsyncTask is deprecated. – Will Sakata Feb 12 '21 at 11:07
  • hello bro, you were able to convert this asyntask I have same issue. I don`t know continuously using asyntask will not affect my app performance in the future or it may cause a lot of app crash. – Clinton Canarias Sep 27 '21 at 21:38

0 Answers0