10

我正在为 Google Android 开发一个简单的应用程序来打开和关闭 wifi 或 3g 或 2g。

我看到http://developer.android.com/reference/android/net/wifi/WifiManager.html#isWifiEnabled() 你可以看到 wifi 是启用还是禁用,还有我们 http://developer.android.com /reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean) 打开和关闭 wifi。

我想知道是否可以为 3G 和 2G/GPRS 做同样的事情?我知道这是可能的,因为您可以关闭 3G 并打开 2G。

4

2 回答 2

13

2G/3G

要确定您的网络类型,请使用:

TelephonyManager.getNetworkType();

这是一些示例代码:

bool is3G = (manager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);

该课程的文档可以在以下位置找到:TelephonyManager


开关

要检查您的电话收音机是打开还是关闭,请使用:

ServiceState.getState();

设置它使用:

ServiceState.setState(STATE_POWER_OFF);

目前尚不清楚 setState 方法是否存在于所有状态下的所有设备和功能上。没有此方法的文档。该类的文档可以在以下位置找到:ServiceState

这个问题也可能是相关的:http ://code.google.com/p/android/issues/detail?id=1065

于 2009-04-18T19:14:09.990 回答
2

您还可以使用 ConnectivityManager。像这样的东西:

ConnectivityManager connectivityManager =(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
于 2009-05-05T09:06:59.670 回答