1

I have written service as library project and connected that service to another application.now i want to pass some input values to service.how can I pass?

Pass values like this:

Intent intent = new Intent(SelfMonitor.this, SchedulerService.class);
intent.putExtra("initialDelay", initialDelay);
intent.putExtra("endTime", endTime);
startService(intent);

Get value like this in service:

initialDelay = Integer.parseInt(intent.getStringExtra("initialDelay"));
endTime = intent.getStringExtra("endTime");

but i am getting null pointer exception?

nobalG
  • 4,544
  • 3
  • 34
  • 72
subash
  • 47
  • 11

2 Answers2

0

Like you do in your activites,

 Intent intent =  new Intent(Activity.this, YourService.class);
    intent.putExtra("key", value);
    startService(intent);

and in your service

Bundle extras = getIntent().getExtras();
if (extras != null)
{
value = extras.getString("key", "somestring");
}

For further details,please see this

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
0
You need serviceIntent for this purpose.

Intent serviceIntent = new Intent(Activity.this, CheckAPI.class);
serviceIntent.putExtra("number", "96999");
serviceIntent.putExtra("message", "hello world");         
startService(serviceIntent);    
Nitesh
  • 7
  • 4