Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynctask strange behaviour

I met some strange warning with asynctask.I'm learning GCM so I try to use the asynctask. this is the warning:

Type safety: The method execute(Object...) belongs to the raw type AsyncTask. References to generic type AsyncTask<Params,Progress,Result> should be parameterized

I want to understand the meaning of this warning.IS it about threads .why it not safe? and what is this raw?

this is my code:

private void registerInBackground() {
    // TODO Auto-generated method stub



    new AsyncTask() {
        @Override
        protected Object doInBackground(Object... arg0) {
            // TODO Auto-generated method stub
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.
                // The request to your server should be authenticated if your app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the regID - no need to register again.
                storeRegistrationId(getApplicationContext(), regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }   @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(), ""+result.toString(), Toast.LENGTH_LONG).show();
        }
    }.execute(null, null, null);



}

Thanks

like image 650
Inès Belhouchet Avatar asked Dec 19 '13 10:12

Inès Belhouchet


2 Answers

AsyncTask is a generic Class that can parameterized. The warning means that you should explicitly name the parameters.

The AsyncTask is defined as AsyncTask<Params,Progress,Result>, it needs 3 Parameters Params, Progress and Result that can be any Type/Class.

In your code above you return a String in doInBackground(). The return value of doInBackground() is the third parameter of the AsyncTask (Result).

To make the warning go away, implement your AsyncTask like this:

new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... voids) {
            String message = "";

            //do something

            return message;
        }

        @Override
        protected void onPostExecute(String result) {

        }
    }.execute();
like image 133
thaussma Avatar answered Nov 08 '22 16:11

thaussma


Usually AsyncTask is used/instantiated with parameters ... AsyncTask<Params, Progress, Result>. What the parameters signify are:

  • Params:
  • They are Objects that you pass into the doInBackground(). For example if I initialize the Params to be a String, I can now access the string parameter with in my doInBackground(String... params) from params[0], params[1], etc. This is one of the most important parameter that you specify because on calling the AsyncTask you can send in the parameter needed in the doInBackground thread. like new AsyncTask().execute(string);

  • Progress:
  • When using the AsyncTask one usually occupies the activity for loading content etc. You can use the progress parameter to give context to the user's that things are going the way they should. Suppose you are blocking the user interface telling your user that it's downloading content. With this progress you can specify how much of your content has been downloaded. If for example this parameter is an integer I could call the publishProgress() in the doInBackground thread to show progress on the content currently being downloaded.

  • Result:
  • Usually this is the only parameter that is allowed to interact with the user interface. Suppose you are done downloading your content. And would want to specify that the operation is complete displaying a success message. This is where you do it. For example, you would like to return a String saying that the download is now complete. In your doInBackground thread you return string; at the end. This value is thus passed into the onPostExecute(String result). From there you could set textView.setText(result).

    Basically AsyncTask is based on the principal of GENERICS and VARARGS (java concepts)

    GENERICS implying that the parameter in AsyncTask can be of any type and VARARGS specifying that you can send in multiple values inside these parameters.

    In you case, since you are not doing anything pretty significant to change the UI you could call AsyncTask<Void, Void, Void>. For better understanding on the workings of the AsyncTask you could see the following example.

    It's a long read but hope this clears up things :)

    like image 32
    Rakeeb Rajbhandari Avatar answered Nov 08 '22 17:11

    Rakeeb Rajbhandari