Android在代码中打开Wifi、移动网络和GPS

以下方法前2个在2.3.5和4.1.1中测试成功,第3个方法在2.3.5中测试没问题,但在4.1.1中无效,待解决。详见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方法,查看源代码发现,它是调用IConnectivityManager类中的setMobileDataEnabled(boolean)方法。由于方法不可见,只能采用反射来调用:
/**  
 * 移动网络开关  
 */ 
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也比较麻烦,与打开移动网络一样,没有直接的方法。在网上搜索了一下,据说采用反射去调用系统的方法仍然会失败,有网友用另外一条路实现了同样的功能,在2.3.5系统测试可以通过,在4.1.1系统中测试无效,待解决:
/**  
 * <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();  
    } 
}


 
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值