android 如何实现移动数据上网的开关?之前百度了一下,找到几篇,但都说不行,今天再搜索,想试一试,竟都不见了!特来此求肋。谢谢
10条回答 默认 最新
- usniyo 2012-10-09 07:25关注
感谢名位的回答,虽然没有用到,但也是学到了些东西的。本人已经找到了解决办法,也在此分享吧:
解决办法中使用到了ConnectivityManager类,但是在SDK中的ConnectivityManager类并没有提供setMobileDataEnable()、getMobileDataEnable()等方法。也看了ConnectivityManager类的源码,里面都是有这些方法的。弄了大半天,最终狠下心来,自己写个ConnectivityManager类,其中的方法和源ConnectivityManager类的方法一样,但都是空实现,编译成ConnectivityManager.class文件,然后替换android.jar包中的android.net.ConnectivityManager.class文件。然后在项目中,使用ConnectivityManager的setMobileDataEnable()方法来打开和关闭数据上网,使用getMobileDataEnable()方法获得当前连接状态,当然这需要android.permission.CHANGE_NETWORK_STATE和android.permission.ACCESS_NETWORK_STATE权限。
ConnectivityManager类如下:package android.net; import java.net.InetAddress; public class ConnectivityManager { public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; public static final String CONNECTIVITY_ACTION_IMMEDIATE = "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE"; public static final String EXTRA_NETWORK_INFO = "networkInfo"; public static final String EXTRA_IS_FAILOVER = "isFailover"; public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; public static final String EXTRA_REASON = "reason"; public static final String EXTRA_EXTRA_INFO = "extraInfo"; public static final String EXTRA_INET_CONDITION = "inetCondition"; public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; public static final String INET_CONDITION_ACTION = "android.net.conn.INET_CONDITION_ACTION"; public static final String ACTION_TETHER_STATE_CHANGED = "android.net.conn.TETHER_STATE_CHANGED"; public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; public static final String EXTRA_ACTIVE_TETHER = "activeArray"; public static final String EXTRA_ERRORED_TETHER = "erroredArray"; public static final int TYPE_NONE = -1; public static final int TYPE_MOBILE = 0; public static final int TYPE_WIFI = 1; public static final int TYPE_MOBILE_MMS = 2; public static final int TYPE_MOBILE_SUPL = 3; public static final int TYPE_MOBILE_DUN = 4; public static final int TYPE_MOBILE_HIPRI = 5; public static final int TYPE_WIMAX = 6; public static final int TYPE_BLUETOOTH = 7; public static final int TYPE_DUMMY = 8; public static final int TYPE_ETHERNET = 9; public static final int TYPE_MOBILE_FOTA = 10; public static final int TYPE_MOBILE_IMS = 11; public static final int TYPE_MOBILE_CBS = 12; public static final int TYPE_WIFI_P2P = 13; public static final int MAX_RADIO_TYPE = TYPE_WIFI_P2P; public static final int MAX_NETWORK_TYPE = TYPE_WIFI_P2P; public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; public static boolean isNetworkTypeValid(int networkType) { return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; } /** {@hide} */ public static String getNetworkTypeName(int type) { return Integer.toString(type); } /** {@hide} */ public static boolean isNetworkTypeMobile(int networkType) { return false; } public void setNetworkPreference(int preference) { } public int getNetworkPreference() { return 0; } class NetworkInfo { } public NetworkInfo getActiveNetworkInfo() { return null; } /** {@hide} */ public NetworkInfo getActiveNetworkInfoForUid(int uid) { return null; } public NetworkInfo getNetworkInfo(int networkType) { return null; } public NetworkInfo[] getAllNetworkInfo() { return null; } /** {@hide} */ public LinkProperties getActiveLinkProperties() { return null; } /** {@hide} */ public LinkProperties getLinkProperties(int networkType) { return null; } /** {@hide} */ public boolean setRadios(boolean turnOn) { return turnOn; } /** {@hide} */ public boolean setRadio(int networkType, boolean turnOn) { return turnOn; } public int startUsingNetworkFeature(int networkType, String feature) { return networkType; } public int stopUsingNetworkFeature(int networkType, String feature) { return networkType; } public boolean requestRouteToHost(int networkType, int hostAddress) { return false; } public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { return false; } @Deprecated public boolean getBackgroundDataSetting() { return true; } @Deprecated public void setBackgroundDataSetting(boolean allowBackgroundData) { } public NetworkQuotaInfo getActiveNetworkQuotaInfo() { return null; } public boolean getMobileDataEnabled() { return false; } public void setMobileDataEnabled(boolean enabled) { } class Context { } public static ConnectivityManager from(Context context) { return new ConnectivityManager(); } public String[] getTetherableIfaces() { return null; } public String[] getTetheredIfaces() { return null; } public String[] getTetheringErroredIfaces() { return null; } public int tether(String iface) { return 0; } public int untether(String iface) { return 0; } public boolean isTetheringSupported() { return false; } public String[] getTetherableUsbRegexs() { return null; } public String[] getTetherableWifiRegexs() { return null; } public String[] getTetherableBluetoothRegexs() { return null; } public int setUsbTethering(boolean enable) { return 0; } /** {@hide} */ public static final int TETHER_ERROR_NO_ERROR = 0; /** {@hide} */ public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; /** {@hide} */ public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; /** {@hide} */ public static final int TETHER_ERROR_UNSUPPORTED = 3; /** {@hide} */ public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; /** {@hide} */ public static final int TETHER_ERROR_MASTER_ERROR = 5; /** {@hide} */ public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; /** {@hide} */ public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; /** {@hide} */ public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8; /** {@hide} */ public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9; /** {@hide} */ public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; public int getLastTetherError(String iface) { return 0; } public boolean requestNetworkTransitionWakelock(String forWhom) { return false; } public void reportInetCondition(int networkType, int percentage) { } class PointerProperties { } public void setGlobalProxy(PointerProperties p) { } public PointerProperties getGlobalProxy() { return null; } public PointerProperties getProxy() { return null; } public void setDataDependency(int networkType, boolean met) { } public boolean isNetworkSupported(int networkType) { return false; } public boolean isActiveNetworkMetered() { return false; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 vscode platformio
- ¥15 代写uni代码,app唤醒
- ¥15 全志t113i启动qt应用程序提示internal error
- ¥15 ensp可以看看嘛.
- ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
- ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
- ¥15 minist数字识别
- ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
- ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
- ¥15 网上下载的3DMAX模型,不显示贴图怎么办