我被这个问题阻止了几天,找不到答案。
我从"活动"启动远程服务。启动服务后,我关闭启动"活动"。(我按下"活动"中的按钮以启动远程服务)。
发生的事情是我的服务重新启动了!当我关闭启动"活动"时。我将启动"活动"放在后台,从而进入onPause()状态,然后从任务管理器关闭它。
当我这样做时,我的远程服务将重新启动,并在大约2秒钟后再次打开。所有的内部变量都将被重置,我不希望这样。
-我不想使用useForeground()方法,因为我不希望我的服务在Notification中可见
-我还想将onBind()与服务一起使用
我是这样声明的:
我目前启动了这样的远程服务,服务启动(我可以在服务区域看到它):
//button clicked form onCreate() launching Activity
btnStartSrv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startService(new Intent("RM_SRV_AIDL"));//the name of the service
}
});
在AndroidManifest.xml中,我这样声明了远程服务:
<service
android:name="com.mainActivity.MyRemoteService"
android:label="somenameService"
android:process=":remote"
>
<intent-filter>
<action android:name="RM_SRV_AIDL" />
</intent-filter>
</service>
如何解决重新启动问题?
我不知道你真正想要什么,因为你说你想要onBind()
,但你的代码片段中没有,而且你使用startService()
,这通常是启动Services
的另一种方式。
然而,这些可能会对您有所帮助:
"服务既可以启动,也可以绑定连接。"因此,您不必在绑定的Service
(onBind()
)或明确启动/停止的startService()
之间进行选择。
另外,不要忘记在从onStartCommand (Intent intent, int flags, int startId)
开始时设置Service FLAGS
,就像START_STICKY
一样。
希望这些能有所帮助。