2

I have an app that use with web service.

The action with the web service maybe will take a few seconds, when i search in google a way for handler with this concept i saw that i can use AsyncTask or new Thread.

I explain my basic action app. In the MainActivity i have two fields username and password. The MainActivity get from the user the inputs and send those inputs to function in ActionClass and there I use with WSClass. In the function class i sent to web service and wait to the response.

  1. How can i create the white circle when the user wait to my actions app?
  2. What the best option to use?(AsyncTask/new Thread)

MainActivity:

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ActionClass = ActionClass.getInstans();
        ActionClass.setContext(this);
        ActionClass.login("username","password");// in the future in put a PlainText for username and password.
        if(isLogin == true)
        {
            //here i move to another activity....
        }
    }
}

public class ActionClass
{
    public void setContext(Context context) {
        if (this.context == null) {
            this.context = context;
            WSClass = new WSClass();
        }
    }
  public static ActionClass getInstans() {
        if (instans == null) {
            synchronized (ActionClass.class) {
                if (instans == null)
                    instans = new ActionClass();
            }
        }

        return instans;
    }

    public boolean login(String username, String password) {
    {
        WSClass.loginWs(url,username, password, object.class);
    }
}

     public class WSClass
     {
          public <T> T login(String URL,String username,String  password, 
          Class<T> output)
          {

          //...Do Something...
          }
     }

Update the qustion The WsClass use with generic function that get and return <T> variable.

After i checked the AsyncTask... AsyncTask can be use as generic if not how can i resolve this?

The function in WSClass updated above.

Thanks,

Tal

tal
  • 295
  • 1
  • 4
  • 20
  • AsyncTask is the best way. – Jackey kabra Dec 06 '17 at 08:39
  • You should split these up into two questions. – Frederick Eskens Dec 06 '17 at 08:44
  • "Indeterminate Progress Use indeterminate mode for the progress bar when you do not know how long an operation will take. Indeterminate mode is the default for progress bar and shows a cyclic animation without a specific amount of progress indicated." from [developer.android.com](https://developer.android.com/reference/android/widget/ProgressBar.html) – Fildor Dec 06 '17 at 08:45
  • Everything you want is explained in this post https://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread. – Sunil Sunny Dec 06 '17 at 08:50
  • Thanks a lot.. :) – tal Dec 06 '17 at 08:55

2 Answers2

0

In my opinion Using Async Task is better.

Async task is created from Handler and Thread to handle asynchronous purpose. For Handling Some task where execution time is short(for a few seconds) and having background works use Async task always. Async task executes Like an Individual thread without interpreting the UI Thread.

AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

if you just do simple task(in your example,it is do login),you'd better choose AsyncTask,but if you want do some more work,and these work are relative,or the task need restart frequently,you'd better choose thread

kelo
  • 83
  • 1
  • 6