远程服务 - 关闭启动活动后重新启动

时间:2014-02-24 22:49:18

标签: android service android-activity

我被封锁了几天,但却找不到答案。

我从Activity启动远程服务。启动服务后,我关闭启动活动。(我按下活动中的按钮启动远程服务)。

发生的事情是我的服务重新启动!当我关闭启动活动时。 我将启动Activity放在后台,因此进入onPause()状态,然后我从任务管理器关闭它。

当我这样做时,我的远程服务重新启动并在大约2秒后再次启动。 所有内部变量都将被重置,我不希望这样。

- 我不想使用useForeground()方法,因为我不希望我的服务在通知中可见

- 我也希望将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> 

如何通过重启问题?

1 个答案:

答案 0 :(得分:0)

我不知道你真正想要什么,因为你说你想要onBind(),但你的代码片段中没有它,而你使用startService()通常是你开始的另一种方式{ {1}}。

但是这些可能会对您有所帮助:

"A service can be both started and have connections bound to it."因此,您无需在有界ServicesService)或明确启动/停止(onBind())之间进行选择。

另外,请不要忘记在onStartCommand (Intent intent, int flags, int startId)开始时设置startService(),例如START_STICKY

希望这些有所帮助。

相关问题