CodingDecoding CodingDecoding - 2 months ago 24x
Android Question

Calling getIntent Method in service

I have to pass parameter from MyActivity.class to TestService.class. MyActivity is a Activity class and Test Service is a Service that i have made for sending messages. I have to pass parameter from Activity Class to the Service class. but when i call

Intent i = getIntent();
in service class, I am getting an error getIntent() is undefined. Please give me suggestion how can i take parameter from my activity class in service or is there any alternate method to use getIntent(). Thanks in Advance


Start your service like this;

 Intent ir=new Intent(this, Service.class); 
 ir.putExtra("data", data); 

You attach your data as an intent extra.

Then to retrieve the data from the service;

data=(String) intent.getExtras().get("data"); 

So you can access your parameter from either the onHandleIntent or onStartCommand Intent parameter. (depending on which type of service you are running) For Example;


protected void onStartCommand (Intent intent, int flags, int startId) {
    data=(String) intent.getExtras().get("data"); 

public int onStartCommand (Intent intent, int flags, int startId)


protected void onHandleIntent(Intent intent) {
    data=(String) intent.getExtras().get("data"); 

protected abstract void onHandleIntent (Intent intent)