以下方法在2.1中测试成功。理论上2.1以上应该也可以。不过2.1以后的Android版本中已经有提供了相关方法,详见android.provider.Settings.Secure类。
记得在AndroidManifest.xml中声明相关权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
1. 打开WiFi最简单,直接调用系统的方法即可:
/**
* WIFI网络开关
*/
private void toggleWiFi(Context context, boolean enabled) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled(enabled);
}
2. 打开移动网络比较麻烦,系统没有直接提供开放的方法,只在ConnectivityManager类中有一个不可见的setMobileDataEnabled方法,查看源代码发现,它是调用I<wbr><span style="line-height:22px">ConnectivityManager类中的</span><span style="line-height:22px">setMobileDataEnabled(boolean)方法。由于方法不可见,只能采用反射来调用:</span></wbr>
/**
* 移动网络开关
*/
private void toggleMobileData(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null; // ConnectivityManager类
Field iConMgrField = null; // ConnectivityManager类中的字段
Object iConMgr = null; // IConnectivityManager类的引用
Class<?> iConMgrClass = null; // IConnectivityManager类
Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
try {
// 取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager类中的对象mService
iConMgrField = conMgrClass.getDeclaredField("mService");
// 设置mService可访问
iConMgrField.setAccessible(true);
// 取得mService的实例化类IConnectivityManager
iConMgr = iConMgrField.get(conMgr);
// 取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());
// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
// 设置setMobileDataEnabled方法可访问
setMobileDataEnabledMethod.setAccessible(true);
// 调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr, enabled);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
3. 打开GPS也比较麻烦,与打开移动网络一样,没有直接的方法。在网上搜索了一下,据说采用反射去调用系统的方法仍然会失败,有网友用另外一条路实现了同样的功能:
/**
* <p>GPS开关
* <p>当前若关则打开
* <p>当前若开则关闭
*/
private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
分享到:
相关推荐
最近在做一个开启和关闭手机端网络的应用,找了许多资料,都不是很全,而且代码写的很乱,在这里总结一下,希望能帮助你,不用和我一样花那么多时间研究,多留点时间和女朋友那啥吧! 《!苦逼的程序猿!》 说明:...
Android例子源码GPS、GPRS、WIFI状态判断+SIM卡信息读取Android例子源码GPS、GPRS、WIFI状态判断+SIM卡信息读取
Android例子源码GPS、GPRS、WIFI状态判断+SIM卡信息读取.zip
实现51单片机GPRS发送GPS的数据到PC机上,该代码已经测试过可以运行。
android GPRS开启关闭,用代码开启的,同时计算了连接网络的时间。
实现了一键开关的架构,里面包涵WIFI,蓝牙,GPRS,供大家一起学习。。。
四信F7126 GPS+GPRS WIFI ROUTER 无线路由器技术规范zip,提供“四信F7126 GPS+GPRS WIFI ROUTER 无线路由器技术规范”免费资料下载,主要包括产品特点、规格及硬件系统等内容,可供选型参考。
通过代码直接能够开启gprs,4.0之后的经过试验也是可以。
通过代码打开gprs/wifi,但是打开gps不是通过代码打开的,是跳转到页面进行打开,然后通过网络定位和gps定位两种方式获取经纬度!
demo可以根据GPS定位、WIFI定位及GPRS定位
Android例子源码——GPS、GPRS、WIFI状态判断+SIM卡信息读取(获取设备编号、软件版本、运营商代号、运营商名称、手机制式、设备位置等).zip
安卓手机(android)wifi传送文件源码可以实现一个android手机之间 , 手机和PC 之间 可以通过wifi相互传文件的程序 。亲测可用 :) 程序分为 android手机端的 apk 和 Pc端的 exe 两个 文件 。 程序会自己建立一个...
google gps实例demo,几乎涵盖所有方式获取位置信息。gps、wifi\gprs、基站获取(cdma,wcdma,gsm)。难得的例子。
在项目的开发中因为要使用到WIFI和GPRS网络的切换,因此就研究了一下通过代码打开WIFI和GPRS的工作。 无论是切换WIFI还是切换GPRS网络都需要设置相应的权限,所以需要在AndroidManifest.xml文件中加入以下几行代码。...
一个测试GPS连接和GPRS连接的小程序,可驱动GPS和GPRS连接
GPS和GPRS使用命令及相关程序,用到的命令都符合国际标准协议,采用科学的数据结构进行数据处理。
基于GPS的车辆导航及GPRS网络监控 摘 要:本文介绍了一种基于GPS和GPRS结合的电子导航和远程监控系统。GPS模块输出的位置信息数据通过RS-232串行接口发送到微型工控机,工控机上运行的GIS显示出车辆的当前位置,...
[源码]android设备上GPRS的开启和关闭,完整工程,直接可用
这是一个利用gprs 传输gps定位信息的c源代码
GPS+GPRS模块芯片,硬件设计资料,对应AT指令