sachi
sachi

Reputation: 479

How to get the value in my service class which is passed from activity class?

Here is my code to pass the value from activity class to my service class.

Intent i =  new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);

How can i get that rowId in my service class ? Please help me

Upvotes: 1

Views: 3113

Answers (4)

Alex P
Alex P

Reputation: 1

You put extra in Activity

Intent intent = new Intent(this, Service.class); 
intent.putExtra("Key", "String");

You get StringExtra in Service class

String ba;    
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ba = intent.getStringExtra("Key");
    return START_STICKY;
}

Upvotes: 0

user370305
user370305

Reputation: 109257

for this you can

Override onStart() -- you receive the Intent as a parameter,

and

onStart() is deprecated now. onStartCommand(Intent, int, int) should be used instead,

then,

String id = intent.getStringExtra("rowId");

or

Bundle extras = getIntent().getExtras(); 
String rowId;
if (extras != null) {
    rowId = extras.getString("rowId");          
}

Upvotes: 2

Siten
Siten

Reputation: 4533

try:

In the MyAlarmService.class

in onStartCommand method write.

String rid = intent.getStringExtra("rowId", rowId);

you will get the answer.

Upvotes: -1

Lalit Poptani
Lalit Poptani

Reputation: 67296

Try this,

public int onStartCommand (Intent intent, int flags, int startId)
{
     super.onStartCommand(intent, flags, startId);
     String id = intent.getStringExtra("rowId");
}

Upvotes: 6

Related Questions