读书人

【Android】开启封闭GPRS

发布时间: 2012-09-10 11:02:32 作者: rapoo

【Android】开启关闭GPRS

/*** 设置数据使用状态 需要 签名 系统 可以根据 变化* @param true 为可用 false为 不可用* @return 0为 成功 -1为失败*/private int setMobileDataEnabled(Context context, boolean flag) {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);Method setMobileDataEnabl;try {setMobileDataEnabl = cm.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);setMobileDataEnabl.invoke(cm, flag);return 0;} catch (Exception e) {e.printStackTrace();return -1;}}


正如代码注释所言,执行该操作需要系统权限。

另外,这个代码好像无法在虚拟机上与运行。在虚拟机上运行效果会有,即GPRS开关关闭,3G流量信号不再显示,但仍然可以用浏览器上网;我后来手动关闭GPRS开关,也可以继续上网……应该是Google为方便开发者做的小手脚吧……

读书人网 >Android

热点推荐