首页>代码>android设置移动联通电信wap接入点>/SetApn/src/com/bon/setapn/SetApn.java
package com.bon.setapn;

import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * 设置apn
 * @author lei
 *
 */
public class SetApn {
		
		 private TelephonyManager mTelephonyManager;
		 ContentResolver resolver;
		 SharedPreferences sharedPreferences;
		 private final int cmWap = 1;  //移动
		 private final int ctWap = 3;  //电信
		 private final int cuWap = 2;  //联通
		  /**
		   * 构造
		   * @param _mTelephonyManager
		   */
		  public SetApn(TelephonyManager _mTelephonyManager,ContentResolver _resolver, SharedPreferences _sharedPreferences){
			  
			  mTelephonyManager = _mTelephonyManager;
			  resolver = _resolver;
			  sharedPreferences = _sharedPreferences;
		  }
		  /**
		   * 判断手机运营商  
		   * @return
		   */
		    private int simOperators(){
		    	//1 为 移动运营商   2为联通  3为电信
				int operators = -1; 
		         String imsi=mTelephonyManager.getSubscriberId();
		         if(!TextUtils.isEmpty(imsi)){
		        	 if(imsi.startsWith("46001")){
		        		 operators=2;
		        	 }else if(imsi.startsWith("46002")||imsi.startsWith("46000")){
		        		 operators=1;
		        	 }else if(imsi.startsWith("46003"))
		        	 {
		        		 operators=3;
		        	 }
		         }
		        return operators;
		    } 
		    
		    /**
		     * 判断手机sim卡网络状态是否正常
		     * @return
		     */
		  private boolean simStatus(){
			  boolean isSimStatusCorrect = false;
			  int simState = mTelephonyManager.getSimState();  
			  switch (simState) {  
			   
	             case TelephonyManager.SIM_STATE_ABSENT:  //无卡
	            	 isSimStatusCorrect = false;
	                 break;  
	   
	             case TelephonyManager.SIM_STATE_NETWORK_LOCKED:  //需要NetworkPIN解锁
	            	 isSimStatusCorrect = false;
	                 break;  
	   
	             case TelephonyManager.SIM_STATE_PIN_REQUIRED:   //需要PIN解锁
	            	 isSimStatusCorrect = false;
	                 break;  
	   
	             case TelephonyManager.SIM_STATE_PUK_REQUIRED:  //需要PUN解锁
	            	 isSimStatusCorrect = false;
	                 break;  
	             case TelephonyManager.SIM_STATE_READY:   //状态正常  
	            	 isSimStatusCorrect = true;
	                 break;  
	             case TelephonyManager.SIM_STATE_UNKNOWN:  //未知状态
	            	 isSimStatusCorrect = false;
	                 break;  
			  }
			  return isSimStatusCorrect;
		  }
		  /**
		   * 设置apn
		   */
		  public void setWapApn(){
			  if(simStatus()){
				  switch (simOperators()) {
					case cmWap:
						SetCmWap.setWapAPN(resolver, sharedPreferences);
						Log.i("MainActivity", "移动");
						break;
					case ctWap:
						SetCtWap.setWapAPN(resolver, sharedPreferences);
						Log.i("MainActivity", "电信");
						break;
					case cuWap:
						SetCuWap.setWapAPN(resolver, sharedPreferences);
						Log.i("MainActivity", "联通");
						break;
				}
			  }
		  }
}
最近下载更多
jnetzhou  LV1 2019年4月3日
zjrzhong  LV1 2018年1月12日
ly2009158  LV1 2016年3月10日
ahbruse  LV1 2016年1月14日
daniel_gjg  LV25 2015年10月19日
kmcc123  LV1 2015年8月27日
yls  LV1 2015年8月17日
leehove  LV17 2015年3月17日
shiyu001  LV2 2014年5月13日
YonggangYuan  LV1 2013年10月22日
最近浏览更多
杰6666 2022年6月17日
暂无贡献等级
eddie2260  LV2 2022年5月24日
氟西汀来救你  LV2 2022年4月26日
isfrand  LV4 2022年4月25日
a566566  LV9 2020年12月29日
waitingforyou  LV20 2020年7月8日
doremi  LV6 2020年6月18日
bawe11  LV1 2020年6月17日
weixiao  LV6 2020年5月27日
蜡炬成灰  LV6 2020年4月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友