简体   繁体   English

1小时后活动被破坏

[英]Activity destroyed after 1 hour

I'm new to Android development. 我是Android开发的新手。 I'v developed an android application which needs to store the connection/data even after 1 hour. 我已经开发了一个Android应用程序,即使在1小时后也需要存储连接/数据。 Currently I have all the data and the connections(chromecast mediaplayer) in a singleton class. 目前,我在单例课程中拥有所有数据和连接(chromecast mediaplayer)。 But, when the user puts the app into the background for about an hour, the activity is destroyed so the connections,data etc are lost causing my app to crash when re-launched. 但是,当用户将应用置于后台约一个小时时,活动被破坏,因此连接,数据等丢失,导致我的应用在重新启动时崩溃。

I've read up on the android services, Can I use these services to hold the singletons so even when the activities are destroyed I can have data binded back to the views when re-launched? 我已经阅读了android服务,是否可以使用这些服务来容纳单例,所以即使活动被破坏,重新启动时我也可以将数据绑定回视图?

Or is there a way to make sure that the activities are not destroyed when android decides to do a cleanup? 还是有一种方法可以确保当android决定进行清理时不会破坏活动? Please advise 请指教

Thanks. 谢谢。

I think you might misunderstand what an Android application is. 我认为您可能会误解什么是Android应用程序。

Your application is a bunch of components that run in a single Linux process. 您的应用程序是一堆在单个Linux进程中运行的组件。 Components come and go, within that process. 在该过程中,组件来来去去。 You have absolutely no control over the lifecycle of the process itself. 您绝对无法控制流程本身的生命周期。

The answer to part of your question is that "yes" a Service will stick around after an invisible activity is destroyed. 您的问题部分的答案是:是的,服务将在无形活动被销毁后仍然存在。

When an Activity becomes invisible, it gets destroyed. 当一个活动变得不可见时,它会被销毁。 If your process is not doing anything else, then the process is likely to be killed too. 如果您的进程没有执行其他任何操作,那么该进程也有可能被终止。

If your process is also running a Service, it is less likely that it will be killed. 如果您的进程也正在运行服务,则将其杀死的可能性较小。 It is just less likely, though. 不过,可能性很小。 The process will eventually get killed. 该过程最终被杀死。 When it does, your singletons will be gone. 如果这样做,您的单身人士将不复存在。 There is nothing you can do to prevent that. 您无法采取任何措施来防止这种情况。 So the answer to the second part of your question is "no". 因此,问题第二部分的答案为“否”。 You cannot depend on singletons in your service to be around when the process is relaunched 重新启动该过程时,您不能依赖服务中的单例

You might look into using the Application object. 您可能会考虑使用Application对象。 Its lifecycle is roughly the same as that of your process. 它的生命周期与过程的生命周期大致相同。 It will not live forever but it will be around whenever any other component of your application is around (except ContentProviders). 它不会永远存在,但是只要您的应用程序的任何其他组件都存在(ContentProviders除外),它就会存在。

It sounds like you want to keep connectivity to a chromecast device around when your application is in the background. 听起来好像要在后台运行应用程序时保持与chromecast设备的连接。 Obviously services can be helpful but I have a few comments that may come handy: 显然,服务可能会有所帮助,但我有一些建议可能会很方便:

  • Services can be killed by system but based on how you have set them up (eg the return value of onStartCommand()), they can be restarted by the system. 服务可以被系统杀死,但是根据您的设置方式(例如onStartCommand()的返回值),它们可以由系统重新启动。 When that happens, you cannot expect that your dynamic data is still there (for example your singleton). 发生这种情况时,您不能指望您的动态数据仍然在那里(例如您的单例)。 You need to include logic to recreate what you need again (for example, rebuild your singleton) 您需要包括重新创建所需逻辑的逻辑(例如,重建单例)

  • Phone can go to sleep when left for a little while (or user can lock his/her phone), so when phone goes to sleep, wifi may drop after a little while, based on the phone settings and the build on your phone; 手机搁置一会儿可以进入睡眠状态(或用户可以锁定手机),因此,根据手机设置和手机的构造,当手机进入睡眠状态后,wifi可能会在一段时间后掉线; some do this more aggressively and some not (even if you hold a lock, it can still happen). 有些会更积极地执行此操作,有些则不会(即使您持有锁,它仍然可能发生)。 The point is that you have to assume that it may happen. 关键是您必须假设它可能会发生。 Even if you have a service, your Cast connection will go down due to wifi loss, so the proper way to handle things is not to try to keep the connection up all the time (since you can't) but is to have logic to re-establish connection when circumstances is right. 即使您有服务,由于wifi丢失,您的Cast连接也会中断,因此处理问题的正确方法不是试图一直保持连接状态(因为您无法这样做),而是要有逻辑在适当的情况下重新建立连接。 In order to do that, you need to preserve enough information to be able to bring things to the state that they were. 为了做到这一点,您需要保留足够的信息,以便能够将事物恢复到原来的状态。 Your logic should also be intelligent enough not to reconnect if it shouldn't. 您的逻辑还应该足够聪明,如果不能,则不要重新连接。

Android OS can destroy any activity , when it is low at resources it destroys any activities to make more space for other apps. Android OS可以破坏任何活动,如果资源不足,它就会破坏任何活动以为其他应用程序腾出更多空间。 But you can use background service to hold your singleton 但是您可以使用后台服务来保持单身

You can use this link to create your background service 您可以使用此链接创建后台服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM