android 控制设备网络接入点

项目需要使用cmwap来接入网络 :

     1.   判断当前设备是否有网络接入(有那么检测是什么接入方式【wifi? cmnet? cmwap?】)


    2.   如果没有任何接入那么 强制使用GPRS数据接入
      
         2.1   如果强制数据接入后为cmnet接入那么还需要强制转换为cmwap接入
         2.2   如果强制数据接入后为cmwap那么不做任何操作


    3.   如果当前设备为wifi接入那么关闭wifi强制GPRS数据接入【有cmnet和cmwap 两种】
       
         3.1   如果强制数据接入后为cmnet接入那么还需要强制转换为cmwap接入
         3.2   如果强制数据接入后为cmwap那么不做任何操作

  4.  如果当前为GPRS数据接入那么判断是cmnet还是cmwap

         4.1   如果为cmnet接入那么强制转换为cmwap接入
         4.2   如果强制数据接入后为cmwap那么不做任何操作

代码实现如下:


package com.c;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class C extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.main);
		  /**
		   * @author zc* 
		   * @data  2013-04-18* 
		   * @param context* 
		   * 判断当前设备接入网络类型  -1:无网络接入;1:wifi接入;2:cmwap接入;3:cmnet接入
		   * */
		  
		 
		  NetworkUtils networkUtils = new NetworkUtils(this);
		  int apntype =  networkUtils.getAPNType(this);
		  switch (apntype) {
		  case -1:
			  //强制数据连接后有cmnet和cmwap
			  networkUtils.setGprsEnabled("setMobileDataEnabled", true);
			  Toast.makeText(this, "当前设备未连接网络开始强制数据连接", Toast.LENGTH_LONG).show();
			  apntype =  networkUtils.getAPNType(this);
			  if(apntype==3)
			  {
				//如果强制接入的为cmnet那么执行强转为cmwap
				networkUtils.deletecmnet();
			  }
			 break;
		  case 1:
			  networkUtils.setWifi(false);
			  networkUtils.setGprsEnabled("setMobileDataEnabled", true);
			  Toast.makeText(this, "当前wifi接入关闭wifi强制启动数据连接", Toast.LENGTH_LONG).show();
			  apntype =  networkUtils.getAPNType(this);
			  if(apntype==3)
			  {
				//如果强制接入的为cmnet那么执行强转为cmwap
				networkUtils.deletecmnet();
			  }
			  break;
		  case 2:
			  Toast.makeText(this, "当前cmwap接入无需操作", Toast.LENGTH_LONG).show();
			  break;
		  case 3:
			  Toast.makeText(this, "当前cmnet接入强制转换为cmwap", Toast.LENGTH_LONG).show();
			  networkUtils.deletecmnet();
			  break;
		  default:
			break;
		  }
		Button bt = (Button) findViewById(R.id.bt1);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("http://m.10086.cn/s3/channnel/cr/600902000008654943/0026291/0001/");
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);
			  }
		    });
	     }
}

package com.c;

import java.lang.reflect.Method;

import android.content.ContentResolver;
import android.content.Context;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;

public class NetworkUtils {
	private static  Context context;
	protected static Uri APN_LIST_URI;
	private static ConnectivityManager mCM; 
	
	public NetworkUtils(Context c){
		this.context=c;
		// APN列表资源
		this.APN_LIST_URI = Uri.parse("content://telephony/carriers");
		
		
		
	}
	/**
	 1      * @author zcmain
	 2      * 获取当前的网络状态  -1:没有网络  1:WIFI网络2:wap网络3:net网络
	 3      * @param context
	       */ 
  public static int getAPNType(Context context){ 
	int WIFI = 1;
	int CMWAP = 2;
	int CMNET = 3;
	int netType = -1;  
		  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
		  NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
		  if(networkInfo==null) 
		      return netType; 
		  int apnType = networkInfo.getType(); 
		  if(apnType==ConnectivityManager.TYPE_MOBILE){ 
		   if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){ 
		          netType = CMNET; 
		   }else{ 
		          netType = CMWAP; 
		        } 
		  }else if(apnType==ConnectivityManager.TYPE_WIFI){ 
		      netType = WIFI; 
		     } 
		 return netType; 
	} 
	
	
	/**
	 * 是否开启 wifi true:开启 false:关闭
	 * 
	 * 一定要加入权限: <uses-permission
	 * android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
	 * <uses-permission
	 * android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
	 * 
	 * 
	 * @param isEnable
	 */
	public  void setWifi(boolean isEnable) {
		WifiManager mWm = null;
		if (mWm == null) {
			mWm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		 }
        if (isEnable) {
           if (!mWm.isWifiEnabled()) {
               mWm.setWifiEnabled(true);
             }
		} else {
			// 关闭 wifi
			if (mWm.isWifiEnabled()) {
				mWm.setWifiEnabled(false);
			}
		}
   }
	
	
    //检测当前GPRS数据连接是否打开
	public  boolean gprsIsOpenMethod(String methodName) {
		    mCM = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		    Class cmClass  = mCM.getClass(); 
	        Class[] argClasses  = null; 
	        Object[] argObject  = null; 
	        Boolean isOpen = false; 
	        try 
	        { 
	            Method method = cmClass.getMethod(methodName, argClasses); 
	            isOpen = (Boolean) method.invoke(mCM, argObject); 
	        } catch (Exception e) 
	        { 
	            e.printStackTrace(); 
	        } 
	        return isOpen; 
	}
		
	
	/**
	 * 强制设备使用数据连接
	 * 
	 * @param flag
	 * @return
	 */
	//强制设备使用数据连接
	 public  void setGprsEnabled(String methodName, boolean isEnable) 
	    { 
		    mCM = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	        Class cmClass       = mCM.getClass(); 
	        Class[] argClasses  = new Class[1]; 
	        argClasses[0]       = boolean.class; 
	        try 
	        { 
	            Method method = cmClass.getMethod(methodName, argClasses); 
	            method.invoke(mCM, isEnable); 
	        } catch (Exception e) 
	        { 
	            e.printStackTrace(); 
	        } 
	    }
	 public void  deletecmnet(){
			
 	        ContentResolver localContentResolver = context.getContentResolver();
	        Uri localUri = this.APN_LIST_URI;
	        int localCursor = localContentResolver.delete(localUri, "apn LIKE 'cmnet'", null);
			
		 
	} 
	    
	    

}

4.0及以上的听说Google限制了WRITE_APN_SETTINGS权限需要在源码环境下编译或者使用系统签名才可通过。


  需要添加权限:
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />



  


你可能感兴趣的:(wifi,cmnet,CMWAP,android网络接入)