首页 > 代码库 > Android获取运营商信息

Android获取运营商信息

项目上要求做三网外放产品,将三个单网SDK自行整合成三网联合,思路大概是在OnCreate时区分运营商

设定枚举,根据取到的sim卡信息赋予枚举变量,后面三网的一些操作根据枚举进行判定


先设定运营商枚举

	enum CARRIEROPERATOR
	{
		NONE(0),
		MM(1),
		UNIPAY(2),
		EGAME(3);
		
		private int value;
		
	    private CARRIEROPERATOR(int i)
	    {
	        this.value=http://www.mamicode.com/i;>


以下是获取sim卡是哪个运营商

	public int getSimOperatorInfo()
	{
		TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		String operatorString = telephonyManager.getSimOperator();
		
		if(operatorString == null)
		{
			return 0;
		}
		
		if(operatorString.equals("46000") || operatorString.equals("46002"))
		{
			//中国移动
			return 1;
		}
		else if(operatorString.equals("46001"))
		{
			//中国联通
			return 2;
		}
		else if(operatorString.equals("46003"))
		{
			//中国电信
			return 3;
		}
		
		//error
		return 0;
	}

在onCreate函数中调用下面的函数设定运营商

void SetCarrierOperation()
		{
			int simCode = GetSimOperatorInfo();
			switch(simCode)
			{
			case 0:
				carrierOperator = CARRIEROPERATOR.NONE;
				break;
			case 1:
				carrierOperator = CARRIEROPERATOR.MM;
				break;
			case 2:
				carrierOperator = CARRIEROPERATOR.UNIPAY;
				break;
			case 3:
				carrierOperator = CARRIEROPERATOR.EGAME;
				break;
			default:
				Log.e("unity","SetCarrierOperation error");
				break;
			}
		}

文章原创,转载请注明

Android获取运营商信息