getIntent() method is undefined inside of onStartCommand() method of android service
-
07-07-2021 - |
Question
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;
}
Solution
Because the intent is passed as a parameter of the function...
onStartCommand(Intent intent, ...
Bundle extras = intent.getExtras();
OTHER TIPS
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.
There's no getIntent
() method in the Service
class. You can use the intent argument as passed to onStartCommand
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow