如何在 Android 应用程序中使用 3G 连接而不是 Wi-fi? [英] How to use 3G Connection in Android Application instead of Wi-fi?

查看:23
本文介绍了如何在 Android 应用程序中使用 3G 连接而不是 Wi-fi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android 应用程序中使用 3G 连接而不是 Wi-fi?

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

我想连接 3G 连接,是否有任何示例代码可以连接到 3G 而不是 Wi-Fi?

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.

我还没有测试以下代码,但看起来它应该可以工作(修改自 这里)

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

在清单中使用以下权限

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

这是一些打开/关闭wifi的实际代码

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

如果您不想走那条路线,您似乎可以告诉手机您更愿意使用移动数据网络而不是 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 提供了一个功能 setNetworkPreference.此功能并未真正记录在案,因为您可以通过单击链接来判断.不过,我会使用它,因为定义的常量似乎暗示您可以将其设置为 TYPE_MOBILETYPE_WIFI并且还有一个 DEFAULT_NETWORK_PREFERENCE 常量被定义为0x00000001 与 TYPE_WIFI 相同.因此,尝试通过调用

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

然后尝试使用 setNetworkPreference() 函数.

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.

如果您确实使用 setNetworkPreference 函数,最好也将网络首选项设置回其原始值(从 getNetworkPreference 接收)

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)

我希望这会有所帮助.

这篇关于如何在 Android 应用程序中使用 3G 连接而不是 Wi-fi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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