2

I have a service in android, and I need to pass parameters to it..I have no idea how to do it..

I tried to putExtra but it wasn't an intent...I used constructor but it doesn't work with constructors..

aclowkay
  • 3,577
  • 5
  • 35
  • 66

2 Answers2

2

From Activity:

Intent intent = new Intent(this, YourService.class);
intent.putExtra("someKey", someData);
intent.putExtra("anotherKey", anotherData);
startService(intent);

In YourService :

public void onStartCommand(Intent data, int startId, int flags) {
    int data1 = data.getExtra("someKey");
    int data2 = data.getExtra("anotherKey");
}

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
0
public int onStartCommand(Intent intent, int flags, int startId) {
    String[] info = intent.getStringArrayExtra("information");
}
Meghal Shah
  • 405
  • 2
  • 11