Android AsyncTask and Passing Multiple Parameters of Type Object
I am an aspiring android developer thanks in great part to Andela Nigeria and their great effort in bring world class software development to Africa and of course Nigeria. I have been working on improving my applications using the tool StrictMode, and I was very used to doing the following:
@Override
protected Void doInBackground(Parameter... parameters) {
... // Some code
return null;
}
task.execute(parameter);
Then I ran into a very cool situation on one of my projects that required me pass in more than one parameter into AsyncTask execute method.
I had this method:
public static void enableAlarmReceiver(boolean state, Context context) {
Log.d(TAG, "enableAlarmReceiver called");
ComponentName receiver = new ComponentName(context, AlarmReceiver.class);
PackageManager pm = context.getPackageManager();
if (state) {
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
} else {
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
that gets called in the MainActivty class like so:
// enable our BootReceiver when an alarm is set,
// and disable it when an alarm is cancelled
if (PreferenceUtility.getQuizReminderState(this)) {
enableAlarmReceiver(true, this);
} else {
enableAlarmReceiver(false, this);
}
This of course raises a StrictMode tool violation and I wondered (being young at this) how I was going to pass in the classes PackageManager
and ComponentName
. Then I googled and of course StackOverflow came up with this cool technic, rather than pass in those previously mentioned classes I would call that method enableAlarmReceiver(boolean state, Context context)
using the AsyncTask class. Now how is a guy like myself going to achieve this. And to have to pass non-primitive types to task.execute()
method. So here is how I was able to get it done.
Steps 1: Extend the AsyncTask class in the HelperUtility class that has the enableAlarmReceiver method with the following Types in the class signature:
public class HelperUtility extends AsyncTask<Object, Void, Void>{}
Step 2: Implement the doInBackground method:
@Override
protected Void doInBackground(Object... objects) {
Boolean state = (Boolean) objects[0];
Context cContext = (Context) objects[1];
enableAlarmReceiver(state, cContext);
return null;
}
Step 3: Then we call the doInBackground()
method in the MainActivity class:
// enable our BootReceiver when an alarm is set,
// and disable it when an alarm is cancelled
if (PreferenceUtility.getQuizReminderState(this)) {
new HelperUtility().execute(true, this);
} else {
new HelperUtility().execute(false, this);
}
Now with this setup the StrictMode error goes away. I love coding and love the fact that what ever we imagine can be done in code, yes some say “not all is possible” but I say it’s your skill that limts you and nothing more. Happy Coding. Note I was able to pass in boolean and Context and use those in the method call.