0

I have an Activity with an EditText and some checkboxes. After the user inserts a text the text should be sent to the Service and the Service will run in background showing a Toast from time to time.

I am having a really hard time trying to figure out how to send the data(Strings and Boolean values that user inputs through the Activity) to the Service .

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
beLejer
  • 163
  • 1
  • 3
  • 14

2 Answers2

1

Use Intent on Activity put values in puExtra

Intent intent = new Intent(current.this, YourClass.class);
intent.putextra("keyName","value"); 

and then call StartService so the OnStart method call be called.. in service get the values in OnStart by using intent

Bundle extras = getIntent().getExtras();
   if (extras != null) 
   {
     String value = extras.getString("key");
   }
Jamil
  • 5,457
  • 4
  • 26
  • 29
0

Have you taken a look at the Android Documentation For Services?

It explains everything you need to know and more :)

(hint: you must pass an Intent to OnStartCommand())

If you google "pass an intent to a service in android" among the first results you'll find:

Pass data from Activity to Service using an Intent

Community
  • 1
  • 1
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • You have the option to use SharedPreferences too. Might be an easier way for the OP to get started as there's less involved and somewhat more intuitive. – indivisible May 04 '14 at 21:42
  • mbs Do you know an example of the SharedPreferences usage? Thanks – beLejer May 04 '14 at 21:54
  • Martin Marconcini i took a look but it's pointing me exactly where i want to. Reading you link right now, thanks you – beLejer May 04 '14 at 21:55