Reputation: 765
I am trying to send data from an activity to service via intents.
This is the code in my activity class
Intent i= new Intent(this,SampService.class);
i.putExtra("startTimeHour", timePicker1.getCurrentHour());
i.putExtra("endTimeHour", timePicker2.getCurrentHour());
i.putExtra("startTimeMinute",timePicker1.getCurrentMinute());
i.putExtra("endTimeMinute", timePicker2.getCurrentMinute());
startActivity(i);
This is the code in my service class
public class SampService extends Service implements Runnable {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_LONG).show();
String startTimeHour,startTimeMinute,endTimeHour,endTimeMinute;
}
public void onStart()
{
Thread t=new Thread(this);
t.start();
}
public void run() {
}
Upvotes: 1
Views: 1313
Reputation: 14058
SampleService is a Service, you should use startService(i);
instead of startActivity(i);
The data attached to the intent can then be retrieved in the Service.
Upvotes: 4
Reputation: 95646
You don't send data from an Activity to a Service with Intents. The Activity needs to bind to the service and then you call methods on the binder. See http://developer.android.com/guide/topics/fundamentals/services.html and http://developer.android.com/reference/android/app/Service.html
Another option would be to have your Activity send a broadcast Intent with the data and your service could set up a BroadcastReceiver to listen for that.
What are you trying to use the service for? Maybe there is an easier or lighter-weight solution to your problem.
Upvotes: 2