Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未调用服务onCreate()_Android_Android Activity_Service_Unbind - Fatal编程技术网

Android 未调用服务onCreate()

Android 未调用服务onCreate(),android,android-activity,service,unbind,Android,Android Activity,Service,Unbind,我有一个活动Y a,点击一个按钮后,我开始另一个活动B,在其中我使用bindService启动服务,如下所示: context.unbindService(serviceConnection); Intent Intent=newintent(上下文,RadioService.class); bindService(intent、serviceConnection、context.BIND\u AUTO\u CREATE) 在此期间,将调用服务类的onCreate()。现在,当我关闭B的ond

我有一个活动Y a,点击一个按钮后,我开始另一个活动B,在其中我使用
bindService
启动服务,如下所示:

context.unbindService(serviceConnection);
Intent Intent=newintent(上下文,RadioService.class);
bindService(intent、serviceConnection、context.BIND\u AUTO\u CREATE)

在此期间,将调用服务类的
onCreate()
。现在,当我关闭B的
ondestory()
中的活动B时,我将停止服务,如下所示:

context.unbindService(serviceConnection);

我面临的问题是,如果我第二次进入活动B,则不会调用服务类
onCreate()
。为什么会这样呢?当我希望调用
onCreate()
时,可以做些什么。

我通过在
bindService()
之前调用
startService()
解决了这个问题,当你
解除绑定该服务时,系统不会破坏该服务。这就是为什么当您重新进入活动B时服务已经运行,因此未调用
onCreate
时的原因。我如何销毁该服务?您需要调用
stopSelf()
stopService()
。但我使用
bindService
启动我的服务,而不是
startService()
。据我所知,
stopSelf()
stopService()
只有在我们使用
startService()
开始服务时才会起作用。如果我错了,请纠正我。你可以检查这个<代码>停止服务()
是否执行取决于服务的实现方式。