WIFI和移动数据(2G/3G)的打开和关闭的代码实现

简介: MainActivity如下:package cc.vv;import java.lang.reflect.Method;import android.
MainActivity如下:
package cc.vv;

import java.lang.reflect.Method;

import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
/**
 * Demo描述:
 * 1 打开和关闭Wifi
 * 2 打开和关闭设备的移动数据(2G/3G)
 * 
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
 * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 * <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/u011485531/article/details/18016351
 * 2 http://blog.csdn.net/li_star/article/details/38758453
 *   Thank you very much
 */
public class MainActivity extends Activity {
    private Button mOpenWifiButton;
    private Button mCloseWifiButton;
    private Button mOpenMobileDataButton;
    private Button mCloseMobileDataButton;
    private Context mContext;
    private ClickListenerImpl mClickListenerImpl;
    private WifiManager mWifiManager;
    private TelephonyManager mTelephonyManager;
    private ConnectivityManager mConnectivityManager;
    private boolean isMobileDataEnabled=false;
    private Method mMethod;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init(){
		mContext=this;
		mClickListenerImpl=new ClickListenerImpl();
		
		mWifiManager=(WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		mConnectivityManager=(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		mTelephonyManager=(TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		//判断当前手机是否在使用MobileData(移动数据)
		if (mTelephonyManager.getDataState()==TelephonyManager.DATA_CONNECTED) {
			isMobileDataEnabled=true;
		}
		
		mOpenWifiButton=(Button) findViewById(R.id.openWifiButton);
		mOpenWifiButton.setOnClickListener(mClickListenerImpl);
		
		mCloseWifiButton=(Button) findViewById(R.id.closeWifiButton);
		mCloseWifiButton.setOnClickListener(mClickListenerImpl);
		
		mOpenMobileDataButton=(Button) findViewById(R.id.openMobileDataButton);
		mOpenMobileDataButton.setOnClickListener(mClickListenerImpl);
		
		mCloseMobileDataButton=(Button) findViewById(R.id.closeMobileDataButton);
		mCloseMobileDataButton.setOnClickListener(mClickListenerImpl);
		
	}
	
	
	private class ClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.openWifiButton:
				openWifi();
				break;
			case R.id.closeWifiButton:
				closeWifi();
				break;
			case R.id.openMobileDataButton:
				openMobileData();
				break;
			case R.id.closeMobileDataButton:
				closeMobileData();
				break;
			default:
				break;
			}
		}

	}
	
	
	//打开Wifi
	private void openWifi(){
		if (!mWifiManager.isWifiEnabled()) {
			mWifiManager.setWifiEnabled(true);
		}
	}
	
	//关闭Wifi
	private void closeWifi(){
		if (mWifiManager.isWifiEnabled()) {
			mWifiManager.setWifiEnabled(false);
		}
	}
	
	//打开移动数据
	private void openMobileData(){
		if (!isMobileDataEnabled) {
			try {
				mMethod=ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
				mMethod.invoke(mConnectivityManager, true);
				isMobileDataEnabled=true;
			} catch (Exception e) {
			}
			
		}
	}
	
	
	//关闭移动数据
    private void closeMobileData(){
    	if (isMobileDataEnabled) {
			try {
				mMethod=ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
				mMethod.invoke(mConnectivityManager, false);
				isMobileDataEnabled=false;
			} catch (Exception e) {
			}
			
		}
	}
	

}

main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <Button
        android:id="@+id/openWifiButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dip"
        android:text="打开wifi" />
    
     <Button
        android:id="@+id/closeWifiButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/openWifiButton"
        android:layout_marginTop="30dip"
        android:text="关闭wifi" />
     
      <Button
        android:id="@+id/openMobileDataButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/closeWifiButton"
        android:layout_marginTop="30dip"
        android:text="打开移动数据" />
      
       <Button
        android:id="@+id/closeMobileDataButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/openMobileDataButton"
        android:layout_marginTop="30dip"
        android:text="关闭移动数据" />

</RelativeLayout>


相关文章
4G、Wifi、蓝牙所谓的AT指令模式和数据透传模式到底是什么意思?
4G、Wifi、蓝牙所谓的AT指令模式和数据透传模式到底是什么意思?
569 0
|
算法
m基于自适应门限软切换的3G和Wifi垂直切换算法的matlab仿真
m基于自适应门限软切换的3G和Wifi垂直切换算法的matlab仿真
125 2
m基于自适应门限软切换的3G和Wifi垂直切换算法的matlab仿真
|
定位技术 API Android开发
Android如何判断当前网络类型(联网,2g,3g,wifi等)
Android如何判断当前网络类型(联网,2g,3g,wifi等)
482 0
|
安全 算法 数据安全/隐私保护
|
图形学
unity3d检测网络是wifi连接还是数据连接或者已断开连接
using UnityEngine; public class NwtWork : MonoBehaviour { private float time = 0f; void Update() { time += Time.
1486 0
|
Android开发
Android判断设备是否打开WIFI、GPRS数据连接
 代码摘要: import android.content.Context; import android.net.
1062 0