简体   繁体   中英

StartActivityForResult from a Service

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.

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.

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

我认为解决方案是从您的服务启动活动,然后将活动绑定到服务并注册回调

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM