如何以编程方式处理 WiFi 到移动网络的切换?

发布于 2024-12-01 17:14:29 字数 1543 浏览 2 评论 0原文

现在,我的应用程序可以与 WiFi 配合使用,但是当我要使用移动提供商网络时,我的应用程序无法运行。我维护了一项检查网络的后台服务,但我不知道如何处理网络切换 WiFi 到移动设备和移动设备到 WiFi?我不知道如何处理 WiFi 到移动网络的切换,因为 WiFi 已经启用并且我不在 WiFi 覆盖区域;在这种情况下,我想自动切换到移动网络,反之亦然。我的方法如下,但不起作用:

String networkStatus = "disconnected";
            int netType = 0;
            try{
            ConnectivityManager connectivityManager =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager != null ){
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                    if(networkInfo != null){
                        netType = networkInfo.getType();
                        Log.d("Log", "connetion is available");
                    }else {
                        Log.d("Log", "connetion is  not available");
                        return networkStatus;
                    }

                //  if(networkInfo.isAvailable()){  // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){  // New change added here
                        if(netType == ConnectivityManager.TYPE_WIFI)
                            {}
                        else if(netType == ConnectivityManager.TYPE_MOBILE )
                            {}
                            }
                        }
                    }catch(Exception e){
            Log.d("Log", "checkNetworkConnection" + e.toString());
            return networkStatus;
        }

我已经阅读了很多帖子,但仍然不明白。谁能给我任何想法或网址,我可以在其中获得相同的实施方法?

提前致谢。

Right now, I am having application which works with WiFi, but while I am going to mobile providers network my application does not working. I have maintained one background service which checks for network, but im not getting how to handle network switch WiFi to Mobile and Mobile to WiFi? I am not getting how to handle WiFi to mobile network switch because already WiFi is enabled and I am not in WiFi coverage area; in this situation I want to get switched to mobile network automatically and vice-versa. My approach is as follows which is not working:

String networkStatus = "disconnected";
            int netType = 0;
            try{
            ConnectivityManager connectivityManager =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager != null ){
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                    if(networkInfo != null){
                        netType = networkInfo.getType();
                        Log.d("Log", "connetion is available");
                    }else {
                        Log.d("Log", "connetion is  not available");
                        return networkStatus;
                    }

                //  if(networkInfo.isAvailable()){  // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){  // New change added here
                        if(netType == ConnectivityManager.TYPE_WIFI)
                            {}
                        else if(netType == ConnectivityManager.TYPE_MOBILE )
                            {}
                            }
                        }
                    }catch(Exception e){
            Log.d("Log", "checkNetworkConnection" + e.toString());
            return networkStatus;
        }

Already I have read many posts over hear still not getting idea. Can anyone give me any idea or url where I can get same approach to implement?

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-12-08 17:14:29

我得到了解决方案:

String networkStatus = "disconnected";
            int netType = 0;
            try{
            ConnectivityManager connectivityManager =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager != null ){
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                    if(networkInfo != null){
                        netType = networkInfo.getType();
                        Log.d("Log", "connetion is available");
                    }else {
                        Log.d("Log", "connetion is  not available");
                        return networkStatus;
                    }

                //  if(networkInfo.isAvailable()){  // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){  // New change added here
                        if(netType == ConnectivityManager.TYPE_WIFI)
                            {}
                        else if(netType == ConnectivityManager.TYPE_MOBILE )
                            {}
                            }
                        }
                    }catch(Exception e){
            Log.d("Log", "checkNetworkConnection" + e.toString());
            return networkStatus;
        }

I got the solution:

String networkStatus = "disconnected";
            int netType = 0;
            try{
            ConnectivityManager connectivityManager =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager != null ){
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                    if(networkInfo != null){
                        netType = networkInfo.getType();
                        Log.d("Log", "connetion is available");
                    }else {
                        Log.d("Log", "connetion is  not available");
                        return networkStatus;
                    }

                //  if(networkInfo.isAvailable()){  // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){  // New change added here
                        if(netType == ConnectivityManager.TYPE_WIFI)
                            {}
                        else if(netType == ConnectivityManager.TYPE_MOBILE )
                            {}
                            }
                        }
                    }catch(Exception e){
            Log.d("Log", "checkNetworkConnection" + e.toString());
            return networkStatus;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文