如何以编程处理的WiFi移动网络交换机? [英] How to handle WiFi to Mobile network switch programatically?

查看:125
本文介绍了如何以编程处理的WiFi移动网络交换机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有应用程序,它具有WiFi的作品,但是当我将移动运营商网络我的应用程序不工作。我一直保持着它检查网络的一个后台服务,但IM没有得到如何处理网络交换机的WiFi手机和移动到无线网络?我没有得到如何,因为无线网络已启用,我不是在WiFi覆盖区域处理的WiFi移动网络开关;在这种情况下我想要得到自动切换到移动网络,反之亦然。我的做法是因为它是不工作如下:

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;
        }

我曾看了很多帖子在听到那些没有得到的想法。谁能给我任何想法或URL,我可以得到同样的方法来实现?

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?

在此先感谢。

推荐答案

我得到了解决:

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;
        }

这篇关于如何以编程处理的WiFi移动网络交换机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆