Passing parameters to Asynctask

I am using Async tasks to get string from the menu activity and load up some stuff..but i am
not able to do so..Am i using it in the right way and am i passing the parameters correctly?
Please see the code snippet. thanks

  private class Setup extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {

            if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

  • Has anyone used Robotium or Calculon for testing Android apps?
  • How to set staggered grid span count to use available screen width?
  • Expand/Collapse Animation: Small lag || MeasureSpec returns wrong value
  • GridView - sometimes rows are top-aligned, sometimes they're bottom-aligned!
  • the user operation is waiting for background work to complete
  • Is it possible to trigger share menu on smartphones (via HTML/JS)?
  • Related posts:

    Date format conversion Android
    Android AppCompat-v21 vs Pure Material
    How to display gradient effect from top to bottom in android
    how to create a shortcut which leads to a non-launcher activity?
    Cannot resolve constructor ArrayAdapter
    Overlay or hide Toolbar on copy/cut selection
  • How to enable Stay awake in the developer options using ADB?
  • Project Name Issue when Importing Android Projects into Eclipse
  • Focus on EditText in ListView when block descendants (Android)
  • Where should the android service calls and calls to GoogleAPIClient be written while using MVP pattern in android?
  • Do we need to install intel x86 Atom system image for all API levels?
  • Genymotion devices not working in Eclipse on OS X Yosemite
  • 4 Solutions collect form web for “Passing parameters to Asynctask”

    Instead of this i would do

     private class Setup extends AsyncTask<String, Integer, Void> {
    
        @Override
        protected Void doInBackground(String... params) {
        String identifier = params[0];
    
              if (identifier.matches("abc")) {
                    publishProgress(0);
                    db.insert_fri();
                } else if ((identifier.matches("xyz"))) {
                    publishProgress(1);
                    db.insert_met();
                }
            }
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Integer... i) {
            // start the song here
            if (i[0] == 0) {
                song.setLooping(true);
                song.start();
            }
        }
    
        @Override
        protected void onPostExecute(Void res) {
    
        }
    
        @Override
        protected void onPreExecute() {
            // do something before execution
        }
    }
    

    and check for “identifier” before invoking the asynctask to prevent overhead of creating a AsyncTask

    like this

    if (!(getIntent().getExtras().isEmpty())) {
                    Bundle gotid = getIntent().getExtras();
                    identifier = gotid.getString("key");
                   new Setup().execute(identifier);
        }
    

    Avoid adding a constructor.

    Simply pass your paramters in the task execute method

    new BackgroundTask().execute(a, b, c); // can have any number of params
    

    Now your background class should look like this

    public class BackgroundTask extends AsyncTask<String, Integer, Long> {
    
        @Override
        protected Long doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String a = arg0[0];
            String b = arg0[1];
            String c = arg0[2];
            //Do the heavy task with a,b,c
            return null;
        }
        //you can keep other methods as well postExecute , preExecute, etc
    
    }
    

    A simple way is to add a constructor:

    public Setup(String a, Int b) {
        this.a = a;
        this.b = b;
    }
    

    AsyncTask means doInBackground() returns Void, onProgressUpdate() takes Integer params and doInbackground takes… String params !

    So you don’t need (and REALLY shouldn’t) use Intent, since it is meant to be used for passing arguments through Activities, not Threads.

    And as told before, you can make a constructor and a global parameter to your class called “identifier”

    public class Setup...
    {
        private String identifier;
    
        public Setup(String a) {
        identifier = a;
        }
    }
    

    Hoped it could help.
    Regards

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.