(1) From an activity, can I call a specific Method (action) of my IntentService? Using the sample code below, I would like to call ONLY ACTION_BAZ:
public class MyIntentService extends IntentService {
private static final String ACTION_FOO = "com.example.application.action.FOO";
private static final String ACTION_BAZ = "com.example.application.action.BAZ";
private static final String EXTRA_PARAM1 = "com.example.application.extra.PARAM1";
private static final String EXTRA_PARAM2 = "com.example.application.extra.PARAM2";
public static void startActionFoo(Context context, String param1,
String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
public static void startActionBaz(Context context, String param1,
String param2) {
// pretty much identical
}
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
}
}
private void handleActionFoo(String param1, String param2) {
// populates sqldatabase tables from asset .csv file
}
private void handleActionBaz(String param1, String param2) {
// compliments onUpgrade database action by dropping tables in ActionFoo
}
}
(2) From my activity, could/should I call?
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE_TABLE);
// Starts MyIntentService queue for populating Table
final Context svccontext = svccontext.getApplicationContext();
final Intent intent = new Intent(svccontext, MyIntentService.class);
intent.setAction(ACTION_FOO);
svccontext.startService(intent);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// the same as above, but calls method to drop tables
intent.setAction(ACTION_BAZ);
}
But then, will it be a problem that I'm not calling the method with the arguments param1 and param2 that are specified in the IntentService's Method (action) from (1)?
(3) Lastly, in the sample code provided by Google (deveoper.android.com), Will my IntentService always initiate both Methods (actions) Foo and Baz in that order? Please correct my understanding if I'm totally off...
I mean to call myIntentService as a background service to perform database functions that won't delay the UI. I might have
ActionFoo populate tables already created by my MainActivity from an asset .csv file.
ActionBaz could drop those aforementioned tables in the case of my MainActivity's onUpgrade call.
The user could still manually input data in the user-defined tables while the background could handle asset-populated tables.
So my 3rd concern is: Will all calls to myIntentService execute both ActionFoo (populate Tables) followed by ActionBaz (thereby dropping the tables I just created)? Or will my onHandleIntent make sure that only the specified Action (added to the intent) is acted upon?
If I have to explicitly mention both the intent's setAction and the parameters from within my Activity, how are my IntentService's helper methods (i.e. startActionFoo) of any help? They all have to be redefined into the calling activity anyway it seems.