Pass parameters to a service?

Put your problem here if it does not fit any of the other categories.

Pass parameters to a service?

Postby BGH » Wed Apr 15, 2009 10:38 am

I have an activity than invokes a service and I would like to pass some values to that service. In between activities I just pass values by adding extras to the intent and then using getIntent() on the new activity and reading the extras off it.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. getIntent().getExtras();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


But getIntent() isn't a method of the service class. So is there a similar way to pass values from activity to service? I'd prefer to not have to bind it if possible as the service doesn't need to interact with the activity other then getting the initial passed parameters.
User avatar
BGH
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 12, 2009 3:51 am
Location: Melbourne, Australia

Top

Postby tbg » Wed Apr 15, 2009 10:45 am

Code: Select all
Intent svc = new Intent(context, Service.class);
svc.putExtra(Constants.STRING_SESSION_ID, sessionId);
context.startService(svc);


on your service class you override onStart
Code: Select all
@Override
    public void onStart(Intent intent, int startId) {
        if (intent.hasExtra(Constants.STRING_SESSION_ID)) {
            this.sessionId = intent.getStringExtra(Constants.STRING_SESSION_ID);
        }
        super.onStart(intent, startId);
    }
tbg
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 21, 2009 2:29 pm

Postby BGH » Wed Apr 15, 2009 10:47 am

Wow, that was quick.

Thanks heaps man.
User avatar
BGH
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 12, 2009 3:51 am
Location: Melbourne, Australia

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 26 guests