In your SpeechToTextAPI.java, the onCreate method is part of the SpeechToTextService class, which extends IntentService. To retrieve extras from the Intent that started the service, you need to access the Intent object passed to the service.
onCreateIn an IntentService, the Intent is not directly available in the onCreate method. The Intent is passed to the onHandleIntent method. However, if you want to access extras in onCreate, you can use the getIntent() method, but IntentService does not provide direct access to the Intent in onCreate.
If you need to access extras in onCreate, you can override the onStartCommand method (which is called after onCreate and before onHandleIntent) and store the extras in a member variable or pass them to a helper method.
Here’s how you can do it:
onStartCommand to retrieve the Intent and its extras.java@Override public int onStartCommand(Intent intent, int flags, int startId) { Logger.logDebug(LOG_TAG, "onStartCommand"); // Retrieve extras from the Intent Bundle extras = intent.getExtras(); if (extras != null) { // Access your extras here String myExtra = extras.getString("my_extra_key"); Logger.logDebug(LOG_TAG, "Retrieved extra: " + myExtra); } // Call the parent method to ensure the service continues as expected return super.onStartCommand(intent, flags, startId); }
onCreate?Intent is not available in onCreate for IntentService.Intent is passed to onHandleIntent, which is called after onCreate.onCreateIf you really need to use the extras in onCreate, you can:
onStartCommand to store the extras in a member variable.onCreate.javaprivate Bundle mExtras; @Override public int onStartCommand(Intent intent, int flags, int startId) { mExtras = intent.getExtras(); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); if (mExtras != null) { String myExtra = mExtras.getString("my_extra_key"); Logger.logDebug(LOG_TAG, "Retrieved extra in onCreate: " + myExtra); } }
onStartCommand to access extras.onCreate: Store extras in a member variable in onStartCommand and access them in onCreate.Would you like help with a specific extra you want to retrieve?
Le Chat can make mistakes. Check answers. Learn more