android:Activity需要最小化或隐藏,如何保持它的活动
我有一个应用程序,其活动的目的是连接到服务器,保持和监视连接并在断开连接时通知用户。活动有其他对话框及其 UI 以及在连接和监视连接时运行的服务。服务仅从 UI 线程启动和停止。
我的查询:一旦建立连接,服务将启动并且活动将被隐藏。从生命周期我可以理解,隐藏时它将进入 onPause() 阶段。然后,如果设备需要内存,它可能会终止我的活动。如果连接未建立,我可以让它被杀死,但如果建立了,那么我不希望它在用户不知情的情况下被杀死。
如何隐藏活动? 我怎样才能阻止它被杀死?或者,如果它被杀死,如何通知用户相同的信息。 如果活动被终止,服务的条件是什么。我不认为他也会被杀。重新启动应用程序时,这会变得不匹配。
编辑信息: 感谢@indyfromoz 和 Aleadam, @Aleadam,我查看了所有这些链接,这帮助我很好地理解和开发了我的服务。 我确实将服务与活动绑定在一起,并且服务具有返回到活动的存根,并且根据需要,服务将消息发送到活动 - 在某些消息上,只有活动会通知用户,但服务会向活动通知消息。其次,另一个 AsyncTask 也在运行,它也会定期通知活动。如果任何连接丢失,活动会再次启动该任务/服务,通知用户。到目前为止我已经做到了这么多,但我只是担心活动被杀死。 Service 和 ASyncTask 将运行并通知用户,但 UI 不会始终在顶部可见,因此将处于 onPause 阶段。如果在内存不足的情况下系统会终止该活动呢?
您是否想说,如果服务已绑定并且通知将继续,则活动不会被终止,因为它是活动的,但不可见?我有点困惑、担心并且不清楚他们处理我申请的这个阶段的任何要求。如果是这样,怎么办?为了更好地理解,下面给出了我的应用程序流程。
流动 : 活动开始 一些输入、验证、UI 等 启动&绑定服务 - startService(bindIntent); bindService(bindIntent, sevConn, 标志); 服务启动/绑定成功?启动 AstncTask :重试 &通知(重试限制)
AsyncTask 已启动 现在 UI 不需要位于顶部,后台工作正在
AsyncTask 和 AsyncTask 上进行。服务提供由活动中的处理程序处理的通知。在任何 sp 消息上向用户显示,并可能显示 UI。
谢谢
I have an application whose Activity's purpose is to connect to a server, keep and monitor the connectivty and on disconnection inform the user about it. Activity has other dialogs and its UI and also a service running while the connecting and monitoring connectiivty. Service starts and stops from te UI thread only.
My query : once connection is established the service will be started and activity will be hidden. From lifecycle I can understand on hidding it will go to the onPause() stage. And then if device needs memory it can kill my activity. If the connection is not established I can let it be killed, but if established, then I don't want it to be killed without user's knowledge.
How to hide the Activity ?
How can I stop it from being killed ? OR if it is being killed how to inform user for the same.
If activity is killed what will be the condition of service. I don't think tha will also be killed. That becomes mismatch on restart of application.
EDIT INFO :
Thanks @indyfromoz and Aleadam,
@Aleadam, I had a look at all those links and that helped me understand and develop my service nicely.
I do bind the servie with the Activity and the service has stub that is returned to the Activity, And as required service sends message to the Actiivty - on some msgs only activity will inform the user but messages are notified by the service to the activity. Secondly another AsyncTask is also running which will also notify activity on regular intervals. And if any connection is lost, Activity handles to start that task/service again informing the user. I have managed this much so far, but am just concerned about the killing of activity. Service and ASyncTask will be running and notifing user, but UI wont be visible all time on top, so that will be in onPause stage. If in low memory case system kills the activity then ?
Do you try to say that if service is binded and notification will be going on then activity wont be killed as it is active though not visible ? I am little bit confused, concerned and don't understand clearly is their any req to handle this stage of my application. If so, how ? For better understanding have given my flow of the app below.
Flow :
Activity start
Some input, validation, UI, etc
Start & Bind Service - startService(bindIntent); bindService(bindIntent, sevConn, flag);
Service started/bounded successfuly ? start AstncTask : Re-Try & inform (re-try limits)
AsyncTask Started
NOW UI IS NOT REQD TO BE ON TOP, BACKGROUND WORK IS GOING ON
Both AsyncTask & Service gives Notifications that are handled by Handlers in Activity. On any sp msg display to user and maybe show UI back.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该服务可以检查 Activity 是否正在运行(通过 IBinder)并启动一个新的 Activity,以防它被杀死,请参阅我的回复 此处 获取相关教程的链接。
另一种可能性是创建一个引用 Activity 和 Service 的 Application 对象,您可以使用它来管理您的需求。应用程序比活动更不可能被杀死。不管怎样,如果你的 Activity 被杀死了,这一切都取决于重新启动它。使用 saveInstance() 保存 Activity 中所需的任何数据(请参阅 此问题)
The Service could check to see if the Activity is running (via an IBinder) and start a new one in case it was killed see my response here for links to tutorials on this.
Another possibility is to create an Application object that has references to both the Activity and the Service, that you can use to manage your needs. Applications are less likely to be killed than Activities. Anyhow, it all comes down to restart your Activity if it was killed. Use saveInstance() to save any data you need from your Activity (See this question)