Android 应用在后台
Android app in the background
我在android studio 开发一个应用程序,我必须保存设备的位置,我做了一个"locationService" class 扩展服务,所以位置是在我的服务器上分段,服务将在后台运行。
我已经阅读了 android 文档并且有两种类型的服务(服务和链接服务),我使用链接服务是因为我需要在 activity.
中显示位置数据
问题是当我关闭应用程序时,服务停止并且不记录位置的变化。
如何防止链接服务死亡。谢谢
我假设你的意思是 "bound" Service
,而不是 "linked" Service
。当最后一个客户端断开连接时,绑定 Service
将停止 运行。为防止这种情况,您需要调用 startService()
并确保您 return START_STICKY
来自 onStartCommand()
在您的 Service
.
您不需要使用 "bound" Service
来将数据从 Service
传递到 Activity
。另一种方法是让您的 Service
广播数据(通过将数据作为 "extra" 添加到 Intent
并使用 Intent
调用 sendBroadcast()
)。然后,您的 Activity
可以设置一个 BroadcastReceiver
来收听您的 Service
的任何数据广播。
我在android studio 开发一个应用程序,我必须保存设备的位置,我做了一个"locationService" class 扩展服务,所以位置是在我的服务器上分段,服务将在后台运行。 我已经阅读了 android 文档并且有两种类型的服务(服务和链接服务),我使用链接服务是因为我需要在 activity.
中显示位置数据问题是当我关闭应用程序时,服务停止并且不记录位置的变化。
如何防止链接服务死亡。谢谢
我假设你的意思是 "bound" Service
,而不是 "linked" Service
。当最后一个客户端断开连接时,绑定 Service
将停止 运行。为防止这种情况,您需要调用 startService()
并确保您 return START_STICKY
来自 onStartCommand()
在您的 Service
.
您不需要使用 "bound" Service
来将数据从 Service
传递到 Activity
。另一种方法是让您的 Service
广播数据(通过将数据作为 "extra" 添加到 Intent
并使用 Intent
调用 sendBroadcast()
)。然后,您的 Activity
可以设置一个 BroadcastReceiver
来收听您的 Service
的任何数据广播。