如果屏幕关闭并与充电器断开连接,活动线程会发生什么情况?
我有一个在服务器和设备之间维护的用户会话上运行的应用程序。我使用 ping 技术来保持设备和服务器之间的会话处于活动状态,如果用户登录,则每 1 分钟对服务器执行一次 ping 操作。这是使用后台线程完成的。
有时,当我关闭屏幕并断开设备与电源的连接时,后台线程会被终止。
有时,线程处于活动状态,但我的 WiFi 天线被关闭,我的 ping 失败。
我的做法正确吗?有更好的方法吗?
我的要求:出于各种原因,我需要每分钟 ping 我的服务器以保持会话处于活动状态,并且只有在用户登录时我才应该执行此操作。
I have an application which works on a user's session which is maintained between the server and the device. I using a ping technique to keep the session alive between the device and server by pinging the server every 1 min if the user is logged in. This is done using a background thread.
Sometimes when I turn OFF the screen and disconnect the device from its power source the background thread gets killed.
Sometimes the thread is active but my WiFi antennae gets turned OFF and my ping fails.
Is my approach correct? Is there a better way of doing this?
My requirement: I need to ping my server every minute to keep the session alive for various reasons and I should do this only if the user is logged in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用它来检查您的 wi-fi 连接,
使用它来打开 wi-fi
如果 checkInternetConnection() == false 您可以在清单中使用它并在代码中
我希望这对您有帮助,尽管这不是您的确切答案问题。
You can use this to check your wi-fi connection
if checkInternetConnection() == false you can turn on wi-fi using this in your manifest
and this in your code
I hope this help you, though this is not an exact answer to your question.
当
Activity
生命周期事件发生时,例如onPause
,使用Log.d
将消息写入Logcat
,onStop
、onDestroy
、onCreate
、onStart
和onResume
。另外,创建一个BroadcastReceiver
来记录广播事件也是最有帮助的。Use
Log.d
to write messages to theLogcat
when anActivity
life cycle event occurs, such asonPause
,onStop
,onDestroy
,onCreate
,onStart
andonResume
. Also creating aBroadcastReceiver
to log broadcast events is most helpful.