如何使用3G连接的Andr​​oid应用程序,而不是Wi-Fi的?如何使用、应用程序、而不是、Andr

2023-09-11 11:45:20 作者:婲謝嘞漃瘼╮

如何使用3G连接的Andr​​oid应用程序,而不是无线网络?

How to use 3G Connection in Android Application instead of Wi-fi?

我要连接3G连接,请问有什么样code连接到3G,而不是无线网络?

I want to connect a 3G connection, is there any sample code to connect to 3G instead of Wi-fi?

推荐答案

在T-Mobile的我的帐户的应用程序是这种情况,如果你连接到WiFi连接,它会告诉你,他们的程序将无法正常工作通过WiFi然后询问用户是否要关闭WiFi连接。如果你选择否,那么应用程序退出,如果你选择是,然后应用程序关闭您的WiFi连接,然后与启动继续。

The T-Mobile 'My Account" app does this, if you are connected to a WiFi connection it tells you that their program will not work over WiFi and then asks the user if they want to turn off the WiFi connection. If you choose "No" then the application exits, if you choose "Yes" then the app turns off your WiFi connection and then continues with starting up.

我觉得这是一个很好的模式可以遵循,这将确保您的应用程序不被跑过来的WiFi,并允许用户决定是否要关闭WiFi与否。这个模型的改进是将打开WiFi回来当用户离开你的应用程序上。

I think this is a good model to follow, it will ensure that your app is not being ran over WiFi and allows the user to decide if they want to turn off WiFi or not. An improvement on this model would be to turn wifi back on when the user navigates away from your app.

我没有测试过以下code,但它看起来像它应该工作(从here)

I haven't tested the following code, but it looks like it should work (modified from here)

使用下面的权限在你的清单

use the following permissions in your manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

和这里是一些实际的code打开无线开/关

and here is some actual code to turn wifi on/off

private WifiManager wifiManager;

@Override 
public void onCreate(Bundle icicle)
{
    ....................

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    else
    {
        wifiManager.setWifiEnabled(true);
    }
}

如果您不希望走这条路线,它看起来像你也许能告诉你preFER使用移动数据网络而非WiFi网络的手机。

If you do not want to go down that route it looks like you might be able to tell the phone that you would prefer to use the mobile data network rather than the wifi network.

Android的ConnectivityManager提供了一个功能setNetwork$p$pference.这个功能是不是真的记录,你可以,如果你点击链接出来。我会发青与它周围,但因为所定义的常量似乎暗示,您可以设置为TYPE_MOBILE或TYPE_WIFI并有一个DEFAULT_NETWORK_$p$pFERENCE常数以及被定义为00000001这是一样的TYPE_WIFI。因此,尝试将ConnectivityManager获得访问通过调用

The Android ConnectivityManager offers a function setNetworkPreference. This function is not really documented as you can tell if you click the link. I would paly around with it though because the constants that are defined seem to hint that you can set this to either TYPE_MOBILE or TYPE_WIFI and there is a DEFAULT_NETWORK_PREFERENCE constant as well that is defined as being 0x00000001 which is the same as TYPE_WIFI. So try getting access to a ConnectivityManager by calling

Context.getSystemService(Context.CONNECTIVITY_SERVICE);

,然后尝试使用使用setnetwork preference()函数。

and then try using the setNetworkPreference() function.

这似乎并不需要在清单中的任何权限,但它可能需要沿着这些线路的CHANGE_NETWORK_STATE许可或什么的。

It doesn't appear to require any permissions in the manifest but it might require the CHANGE_NETWORK_STATE permission or something along those lines.

如果你起诉使用setnetwork preference功能,它很可能是最好也设置网络preference恢复到原来的值(从getNetwork preference收到)

If you do sue the setNetworkPreference function it would probably be best to also set the Network Preference back to its original values (received from getNetworkPreference)

我希望这有助于。

 
精彩推荐
图片推荐