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 .
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
And handle action in a Service. If it is already running it will not be restarted, otherwise it will be started
Open Transparent activity from service and use BroadcastReceiver in service. Follow the steps in detail.
1. Open transparent activity from Service
// For transparent activity use this code in AndroidManifest.xml
2. Create BroadcastReceiver in Service
3. Register this broadcast in onCreate of Service
4. Unregister this broadcast in onDestroy of Service
5. Do work in Activity by using startActivityForResult and Send broadcast from Activity's (FloatingServiceSupportActivity) onActivityResult
There is no such thing as
OnActivityForResult
inside the scope of aService
.You can read the Android developer guidelines here.
I recently stumbled upon the same issue, how to wire onActivityResult() outcome to a Service for an Activity started from the aforementioned Service. I did not want to store the Activity in a static variable given that Activity leak is a bad practice. So I added a static listener in the Activity handling onActivityResult() that implements an appropriate Interface and instantiated this listener as a private member of the Service class.
The code in a simplified form looks like this:
As you can see I could not avoid static references due to device rotation issues, but I think this solution is more appropriate; albeit verbose.