在没有互联网的情况下连接到 Wi-Fi 时使用蜂窝数据 [英] Use cellular data while connected to Wi-Fi with no internet

查看:435
本文介绍了在没有互联网的情况下连接到 Wi-Fi 时使用蜂窝数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Android 应用程序,该应用程序通过 WiFi 连接到另一台设备,该设备通过套接字连接发送和接收数据.我希望能够在与此设备保持连接的同时使用我的蜂窝数据做其他事情(例如浏览).

I'm creating an Android app that connects to another device via WiFi that sends and receives data through a socket connection. I want to be able to use my cellular data to do other things (such as browsing) while staying connected to this device.

在 iOS 设备上,将网络设置更改为静态并将路由器字段留空似乎可行.但是在我的 Android 设备(运行 Android 7.0 的三星 Galaxy Note 5)上,如果我将其留空,它不会让我保存网络设置.

On an iOS device, changing the network settings to Static and leaving the Router field blank seems to work. But on my Android device (Samsung Galaxy Note 5 running Android 7.0), it won't let me save the network settings if I leave it blank.

我曾尝试使用 Mobiwol、Super Download 和 Speedify 等 3rd 方应用程序(似乎只有 Speedify 有效),但我希望无需这些应用程序即可执行此操作.

I have tried using 3rd party apps like Mobiwol, Super Download, and Speedify (Only Speedify seemed to work), but I want to be able to do this without the need of these apps.

我还尝试在开发人员设置中打开保持移动数据打开"和智能网络开关",它只是切换到我的蜂窝数据,因此我的应用程序无法运行,因为它在技术上没有连接到 WiFi.

I have also tried turning on "Keep Mobile data turned on" in the developer settings, and "Smart Network Switch" which just switches to my cellular data so my app does not work since it is not technically connected to the WiFi.

更新:我在我的应用程序中通过 WiFi 连接时设法让蜂窝网络工作(感谢 Remy Lebeau 和 Android 上连接 WIFI 后如何通过移动网络保持连接?).请参阅下面的代码.

UPDATE: I managed to get cellular to work while connected through WiFi within my app (Thanks to Remy Lebeau and How to stay connected through mobile network after WIFI is connected on Android?). See code below.

现在我希望能够在后台应用程序(例如通知)中使用蜂窝数据,或者如果我想打开浏览器等.有没有办法做到这一点?

NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); 
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); 

ConnectivityManager.NetworkCallback networkCallback = new 
ConnectivityManager.NetworkCallback() {

     @Override
     public void onAvailable(Network network) {
            connectivityManager.bindProcessToNetwork(network)                   
     }
};

connectivityManager.requestNetwork(req.build(), networkCallback);

推荐答案

也许这个片段对你有用,它会检查是否有 wifi 连接来完成这项工作,或者它会通过移动连接来完成

Maybe this snippets works for you, it checks if there is wifi connection to do the job, or it will do it with mobile connection

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isConnected()){
    // If Wi-Fi connected
}

if (mobile.isConnected()) {
    // If Internet connected
}

请务必将此添加到您的清单中:

Be sure to add this to your manifest:

"android.permission.ACCESS_NETWORK_STATE"

"android.permission.ACCESS_NETWORK_STATE"

而thsi是一种检测wifi连接的方法,当return false为else语句时,你可以添加移动数据作为else语句

And thsi is a method that checks for wifi connection, you can add the mobile data as the else statment when return false is

private boolean checkWifiOnAndConnected() {
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        if( wifiInfo.getNetworkId() == -1 ){
            return false; // Not connected to an access point
        }
        return true; // Connected to an access point
    }
    else {
        return false; // Wi-Fi adapter is OFF
    }
}

这篇关于在没有互联网的情况下连接到 Wi-Fi 时使用蜂窝数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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