Asynchronous weird behavior

I met some strange warning using asynctask. I am learning GCM, so I am trying to use asynthesis. this is a 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. And it's about streams. Why is it not safe? and what is this?

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

+6
source share
3 answers

AsyncTask is a generic class that can be parameterized. A warning means that you must explicitly specify the parameters.

AsyncTask is defined as AsyncTask<Params,Progress,Result> , it needs 3 parameters Params , Progress and Result , which can be any type / class.

In the above code, you are returning a string in doInBackground() . The return value of doInBackground() is the third parameter of AsyncTask ( Result ).

To clear the warning, run AsyncTask as follows:

 new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String message = ""; //do something return message; } @Override protected void onPostExecute(String result) { } }.execute(); 
+3
source

Usually AsyncTask used / created with parameters ... AsyncTask<Params, Progress, Result> . Parameter Values:

Params: These are the Objects that you pass to doInBackground() . For example, if I initialize Params as a String , now I can access the string parameter using doInBackground(String... params) from params[0] , params[1] , etc. This is one of the most important parameters that you specify that when calling AsyncTask you can send the parameter needed in the doInBackground stream. like new AsyncTask().execute(string);

Progress: When using AsyncTask , activity is usually used to load content, etc. You can use the progress parameter to give the context to the user that everything is going as it should. Suppose you are blocking the user interface by telling your user that he is loading content. With this progress, you can indicate how much of your content has been downloaded. If, for example, this parameter is an integer , I could call publishProgress() on the doInBackground stream to show the progress in the currently loaded content.

Result: This is usually the only parameter that is allowed to interact with the user interface. Suppose you are finished loading your content. And I would like to indicate that the operation is completed, showing a message about successful completion. Here you do it. For example, you would like to return a String message stating that the download is complete. In the doInBackground stream doInBackground you return string; in the end. Thus, this value is passed to onPostExecute(String result) . From there you can set textView.setText(result) .

Basically AsyncTask is based on the principle of GENERICS and VARARGS (Java concepts)

GENERICS implies that a parameter in AsyncTask can be of any type and VARARGS, indicating that you can send multiple values ​​inside these parameters.

In this case, since you are not doing anything significant to change the user interface, you can call AsyncTask<Void, Void, Void> . For a better understanding of AsyncTask you can see the following example .

It reads a long time, but hope this clears things up :)

+1
source

Do it with generics. Typing the AsyncTask class AsyncTask AsyncTask<Params, Progress, Result> . The message you receive is related to what you are doing new AsyncTask() . What you should do is something like new AsyncTask<Void, Void, Void>() , since you are not going to use these types anyway.

The API reference contains additional information on this subject. enter link description here

0
source

All Articles