如何到Android应用程序连接到互联网时自动发现网络

编程入门 行业动态 更新时间:2024-10-23 13:22:05
本文介绍了如何到Android应用程序连接到互联网时自动发现网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的基于IE WIFI网络连接的Andr​​oid应用程序/移动Network.it时,我的手机连接到互联网,但是当互联网连接断开,它停止工作(obesely),它仍然停止我的手机再次连接到互联网后,工作的正常工作。

My android application based on network connection i.e WIFI/Mobile Network.it works fine when my mobile is connected to internet but when internet connection disconnected it stops working(obesely) and it still stop working after my mobile again connected to internet.

我要自动启动我的应用程序,只要互联网络发现

请帮我先生,谢谢!

Please Help me Sir, Thank you !

推荐答案

您可以使用广播接收器,只要网络可用U可以开始你的application.First创建一个后台服务,并启动服务时,设备启动检查网络状态up.Now在这个服务注册一个广播接收器和监控网络state.If网络可U可以启动应用程序,如果不可用,你可以关闭it.Please参考如下code广播接收者。你满意这个答案,然后不要别忘了投票了。

you can check network state using broadcast receiver whenever network is available u can start your application.First create a background service and start your service when device boot up.Now in this service register a broadcast receiver and monitor network state.If network is available u can start your application and if unavailable you can close it.Please refer code given below for broadcast receiver.If you are satisfied with this answer then don't forget to vote up.

public class BroadCastSampleActivity extends Activity { /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.registerReceiver(this.mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO); if(currentNetworkInfo.isConnected()) { Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show(); } } }; }

更多推荐

如何到Android应用程序连接到互联网时自动发现网络

本文发布于:2023-11-27 20:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   连接到   应用程序   发现   网络

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!