user1839514 user1839514 - 9 months ago 100
Android Question

Handle onActivityResult on a Service

So i have a simple Question , it is Possible to Handle the Method onActivityResult() in a Service if this Activity was Started from the Same Service (Using Intent) ?

In My Case , i want to start SpeechRegnition , Speak , and Get the Result on the Service , Everything on Background (Main Service Start from a Widget) ,

Thanks .

Answer Source

Thanks for a recent downvoting, whoever it was. The previous answer I gave back in 2012 is a total nonsesnse so I decided to write a proper one.

You can not handle Activity result in a Service, but you can pass any data retrieved from onActivityResult() to a Service.

If your Service is already running, you can call startService() with a new Intent handling the event, like so

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CODE && resultCode == RESULT_OK) {

private void notifyService(final Intent data) {
    final Intent intent = new Intent(this, MyService.class);
    intent.putExtra(MyService.EXTRA_DATA, data);

And handle action in a Service. If it is already running it will not be restarted, otherwise it will be started

public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        final String action = intent.getAction();
        if (action != null) {
            switch (action) {
                case ACTION_HANDLE_DATA:
                    // Implement your handleData method. Remember not to confuse Intents, or even better make your own Parcelable
    return START_NOT_STICKY;