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

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

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>


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