How can I send data from the current Activity
to a background Service
class which is running at certain time? I tried to set into Intent.putExtras()
but I am not getting it in Service
class
Code in Activity
class which calls the Service
.
Intent mServiceIntent = new Intent(this, SchedulerEventService.class);
mServiceIntent.putExtra("test", "Daily");
startService(mServiceIntent);
Code in Service
class. I treid to put in onBind()
and onStartCommand()
. None of these methods prints the value.
@Override
public IBinder onBind(Intent intent) {
//Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
//String data = intent.getDataString();
Toast.makeText(this, "Starting..", Toast.LENGTH_SHORT).show();
Log.d(APP_TAG,intent.getExtras().getString("test"));
return null;
}
Your code should be onStartCommand
. If you never call bindService
on your activity onBind
will not be called, and use getStringExtra()
instead of getExtras()
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "Starting..", Toast.LENGTH_SHORT).show();
Log.d(APP_TAG,intent.getStringExtra("test"));
return START_STICKY; // or whatever your flag
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With