简体   繁体   English

在android应用程序的后台运行服务以检查网络连接

[英]running service in background of android app to check for network connection

For my app, I would like to have a service running at all times that the app is loaded that will be checking the availability of a network connection, and as soon as it realizes that a network connection is not available, send a message to the loaded activity so that I can replace the content view of that activity with one that simply states "Network Connection is not available". 对于我的应用程序,我希望有一个在应用程序加载时始终运行的服务,该服务将检查网络连接的可用性,并且一旦意识到网络连接不可用,请向加载了活动,以便我可以用一个简单地指出“网络连接不可用”的活动视图替换该活动的内容视图。 Then when the network connection comes back, have the activity receive another message so it can switch the content view back. 然后,当网络连接恢复时,让活动收到另一条消息,以便可以将内容视图切换回去。 I've looked through several posts on services, and would just like to inquire as the best way to go about this. 我浏览了有关服务的几篇文章,并希望以此为最佳查询方法。 Any tutorials or simple sample code would be awesome too! 任何教程或简单的示例代码也将很棒!

Using a Service for that is not really the best solution. 为此使用Service并不是真正的最佳解决方案。 Better is using a BroadcastReceiver . 更好的是使用BroadcastReceiver You can add one programmatically as shown here , or in the manifest like here . 您可以以编程方式添加一个如图所示这里 ,或像清单在这里

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

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