26

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.

Community
  • 1
  • 1

4 Answers4

18

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.

emote_control
  • 745
  • 6
  • 21
  • 2
    Be very careful with "@android:style/Theme.NoDisplay"... I found that added a 5 second delay between calling startActivityForResult() and the dialog showing up. Switching to "@android:style/Theme.Translucent.NoTitleBar.Fullscreen" made the delay go away. Otherwise, this is what worked for me. – TrevorWiley Oct 30 '15 at 19:55
15

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.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • Ok then i tried to use broadcast receivers. however there i can only catch the intents when the state changed, but if bluetooth is disabled and i want to leaf it disabled, then the state doesn't changes and i'm never notified... – RoflcoptrException Aug 10 '10 at 16:03
  • Sorry, I don't understand what you are saying. Can you re-explain? – Macarse Aug 10 '10 at 16:27
  • Since I can't use startActivityForResult() I tried to use BroadcastReceivers to catch the events if bluetooth state changed. Unfortunately the bluetooth state only changes if e.g. bluetooth isn't enabled and the user clicks yes to enable it. Then my broadcastReceiver gets informed. But if bluetooth isn't enabled and the user clicks no, the the bluetooth state does not change and therefore my broadcastReceiver isn't notified. – RoflcoptrException Aug 10 '10 at 17:19
  • Don't you want to enable BlueTooth? What I am saying is that you can start an activity in the service and then calling startActivityForResult. – Macarse Aug 10 '10 at 18:07
4

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 :)

0ne_Up
  • 471
  • 3
  • 9
  • 24
0

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

Pomagranite
  • 696
  • 5
  • 11