How to prevent an android activity to get killed

2019-01-29 12:26发布

I have developed an application that does require to do constant network activity. This Activity does the network connections thru an AsyncTask, sometime this work being done in background (application not having focus). My application sometimes gets killed after 30 mins, even if the system does not seem to lack resources (memory).

I have read the Activity life cycle in the Application Fundamentals section on developer.android.com, but can't find an answer. What would be the proper way to programmatically prevent my Activty to get killed.

Thank you.

3条回答
来,给爷笑一个
2楼-- · 2019-01-29 12:57

You should use a service instead of using an Activity to do work in the background.

查看更多
男人必须洒脱
3楼-- · 2019-01-29 12:57

Does your application have to be an activity? Can it utilize a background service?

http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

查看更多
够拽才男人
4楼-- · 2019-01-29 13:23

If you use a Service and make sure it stays active (by first starting it and simply not stopping it) the application wont be killed (unless it's extremely low on memory of course).

查看更多
登录 后发表回答