Kevik
Kevik

Reputation: 9361

getIntent() method is undefined inside of onStartCommand() method of android service

Does anyone know why my getIntent() method is undefined for the android service and inside of the onStartCommand() method?

   @Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, startId, startId);

Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("filename");


 return START_STICKY;
}

Upvotes: 2

Views: 8093

Answers (3)

Alexander
Alexander

Reputation: 48272

There's no getIntent() method in the Service class. You can use the intent argument as passed to onStartCommand

Upvotes: 2

Dharmendra
Dharmendra

Reputation: 33996

The getIntent() method is of Activity not of Service. In Activity if you want to get the current Intent then you can call the getIntent() method to get the intent. In service the onStartCommand() method has the parameter intent already so you can fetch the extra data passed with the intent.

You are already getting intent object from the onStartCommand() method. check it.

Upvotes: 2

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28561

Because the intent is passed as a parameter of the function...

onStartCommand(Intent intent, ...
    Bundle extras = intent.getExtras();

Upvotes: 4

Related Questions