1

(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.

Clayton
  • 381
  • 4
  • 13
  • 1
    Intents are not static classes. But using extras, I guess you can then parse it and call what ever you want. Just compare the ACTION. – shkschneider Apr 23 '14 at 13:10

1 Answers1

0

From an activity, can I call a specific Method (action) of my IntentService?

Actions have nothing directly to do with methods. You are welcome to include an action string in an explicit Intent, as you are doing in your first bit of sample code.

From my activity, could I call?

No, because that is not valid Java. It will not compile, as startActionBaz() returns void.

Will my IntentService always initiate both Methods (actions) Foo and Baz in that order?

There is no "Foo" in your source code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I was trying to be less verbose in not including an example of action Foo in my source code, it's identical to action Baz. I also use onHandleIntent - with handleActionBaz just as in the example code provided with the Android SDK. But I'll edit my question to make this more explicit. – Clayton Apr 23 '14 at 14:43