Binding service by BroadcastReceiver

后端 未结 8 1056
春和景丽
春和景丽 2021-02-12 15:32

My Application uses a service that is started by a BOOT_COMPLETE BroadcastReceiver, in run i\'m getting an error

my code:

public class projet extends Br         


        
8条回答
  •  庸人自扰
    2021-02-12 15:57

    According to bindService documentation:

    Note: this method can not be called from a BroadcastReceiver component. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call startService(Intent) with the arguments containing the command to be sent, with the service calling its stopSelf(int) method when done executing that command.

    What I did: (Not sure if it works in all cases) You can create a Service and start that service in your BroadcastReceiver. Inside your service onCreate() method, you can call a method from another class who is in charge of binding. You can pass the context from service to that class and use it like this: ctx.bindService(...)

提交回复
热议问题