java – StartActivityForResult from a Service-ThrowExceptions

Exception or error:

Id like to enable bluetooth like in this example.

However my class isnt a Activity but a Service and therefore I can’t call startActivityForResult. How can i solve this problem?. I know there are other questions that have been answered like

use startActivityForResult from non-activity

but this doesn’t solve my problem because my application consists of the service and nothing else.

How to solve:

Unfortunately you can’t do that.

The only solution I found (hack) is to first open an Activity with a Dialog style and then do the call there.

###

I know this is an older question, but I’ve run into a similar challenge, and my solution was to create an activity with android:theme=”@android:style/Theme.NoDisplay”, and then call startActivityForResult() from that. That creates an invisible activity that can both request and receive the intents, before writing data somewhere and then finishing itself.

###

I think the solution is to start the activity from your service then have the activity bind to the service and register a callback

###

I know this is an older question, but I’ve run into a similar challenge, and my solution was to start the activity using startActivity() instead of startActivityForResult(), and using Intent.FLAG_ACTIVITY_NEW_TASK. Then call startService() from the activity instead of setResult(), then use onStartCommand() instead of onActivityResult().

Binding to the service as @Pomagranite suggested could also work, but that seems more complicated 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *