【发布时间】:2015-10-21 19:38:21
【问题描述】:
我有一个需要从活动中获取一些输入的服务。为了让他们交流,我将活动绑定到我的服务,并且交流效果很好。但是,一旦活动关闭并与服务解除绑定,服务也会停止。 是否可以在不杀死服务的情况下解绑?
这就是我启动服务并绑定它的方式:
Intent serviceIntent = new Intent(this, MyService.class);
//Add user preferences to the service
serviceIntent.putExtra("panel", tbtnBar.isChecked());
serviceIntent.putExtra("sensor", tbtnDown.isChecked());
//Start the service
startService(serviceIntent);
//Bind the service
isBound= bindService(serviceIntent, mConnection,0);
服务在 onDestroy 中未绑定:
@Override
public void onDestroy()
{
if(isBound)
unbindService(mConnection);
super.onDestroy();
}
我四处搜索,所有帖子都只是说在绑定之前启动服务而不使用 BIND_AUTO_CREATE 标志。我似乎正在这样做。
【问题讨论】:
-
“但是,一旦活动关闭并与服务解除绑定,服务也会停止”——您如何确定这一点?
-
当它运行时,我会弹出一个鲜红色的窗口,告诉我它已打开。当服务关闭时,窗口消失。
-
你在哪里添加和删除服务中的“亮红色窗口弹出”?
-
在 onStartCommand() 中添加窗口,在 onDestroy() 中删除它
-
我还使用“实时 CPU”运行看门狗应用程序以查看哪些服务正在运行。当 Activity 被销毁时服务停止。
标签: java android android-service android-service-binding