两种方式判断移动运营商(移动,联通,电信)[原创]


 
/**
 * @author Stay
 *		判断移动运营商
 */
public class NetworkOperater extends Activity {
	private static final String TAG = "MainActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		// if (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)
//		WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
//		NetworkInfo info1 = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//		if (info1.isConnectedOrConnecting()) {
//			wifiManager.setWifiEnabled(false);
//		}
//		Log.i(TAG,
//				"getDetailedState=" + info1.getDetailedState() +"\n" + 
//				"getReason=" + info1.getReason() +"\n" + 
//				"getSubtype=" + info1.getSubtype() +"\n" + 
//				"getSubtypeName=" + info1.getSubtypeName() +"\n" + 
//				"getExtraInfo=" + info1.getExtraInfo() +"\n" + 
//				"getTypeName=" + info1.getTypeName() +"\n" + 
//				"getType=" + info1.getType()  + "\n" 
//		);
//	通过联网方式判断
		NetworkInfo info2 = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		Log.i(TAG,
				"getDetailedState=" + info2.getDetailedState() +"\n" + 
				"getReason=" + info2.getReason() +"\n" + 
				"getSubtype=" + info2.getSubtype() +"\n" + 
				"getSubtypeName=" + info2.getSubtypeName() +"\n" + 
				"getExtraInfo=" + info2.getExtraInfo() +"\n" + 
				"getTypeName=" + info2.getTypeName() +"\n" + 
				"getType=" + info2.getType() 
		);
//		通过MNC判断
		TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
		/** 获取SIM卡的IMSI码 
		 * SIM卡唯一标识:IMSI 国际移动用户识别码(IMSI:International Mobile Subscriber Identification Number)是区别移动用户的标志, 
		 * 储存在SIM卡中,可用于区别移动用户的有效信息。IMSI由MCC、MNC、MSIN组成,其中MCC为移动国家号码,由3位数字组成, 
		 * 唯一地识别移动客户所属的国家,我国为460;MNC为网络id,由2位数字组成, 
		 * 用于识别移动客户所归属的移动网络,中国移动为00,中国联通为01,中国电信为03;MSIN为移动客户识别码,采用等长11位数字构成。 
		 * 唯一地识别国内GSM移动通信网中移动客户。所以要区分是移动还是联通,只需取得SIM卡中的MNC字段即可 
		*/  
		String imsi = telManager.getSubscriberId();  
          "getNetworkOperatorName=" + telManager.getNetworkOperatorName()+"\n" +//直接获取移动运营商名称
		if(imsi!=null){  
		    if(imsi.startsWith("46000") || imsi.startsWith("46002") || imsi.startsWith("46007")){//因为移动网络编号46000下的IMSI已经用完,所以虚拟了一个46002编号,134/159号段使用了此编号  
		        //中国移动  
		    }else if(imsi.startsWith("46001")){  
		        //中国联通  
		    }else if(imsi.startsWith("46003")){  
		        //中国电信  
		    }  
		}  
	}
	
	/**
	 * @author Stay
	 *		网络连接方式更改时
	 */
	private class ConnectionChangeReceiver  extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );   
	        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();   
	        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );   
	        if ( activeNetInfo != null ) {   
	            Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
	        }   
	        if( mobNetInfo != null ) {   
	            Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
	        }   
		}
	}
	
/*联通3G卡  wap連接  
	03-16 16:09:14.754: INFO/MainActivity(977): getDetailedState=CONNECTED
	03-16 16:09:14.754: INFO/MainActivity(977): getReason=apnChanged
	03-16 16:09:14.754: INFO/MainActivity(977): getSubtype=8
	03-16 16:09:14.754: INFO/MainActivity(977): getSubtypeName=HSDPA
	03-16 16:09:14.754: INFO/MainActivity(977): getExtraInfo=uniwap
	03-16 16:09:14.754: INFO/MainActivity(977): getTypeName=mobile
	03-16 16:09:14.754: INFO/MainActivity(977): getType=0
*/
	
	
	/*联通3G卡   3g连接
	03-16 16:13:48.314: INFO/MainActivity(1522): getDetailedState=CONNECTED
	03-16 16:13:48.314: INFO/MainActivity(1522): getReason=apnSwitched
	03-16 16:13:48.314: INFO/MainActivity(1522): getSubtype=3
	03-16 16:13:48.314: INFO/MainActivity(1522): getSubtypeName=UMTS
	03-16 16:13:48.314: INFO/MainActivity(1522): getExtraInfo=3gnet
	03-16 16:13:48.314: INFO/MainActivity(1522): getTypeName=mobile
	03-16 16:13:48.314: INFO/MainActivity(1522): getType=0
*/
	
	/*移动     wap连接
	 03-16 16:20:39.295: INFO/MainActivity(734): getDetailedState=CONNECTED
	03-16 16:20:39.295: INFO/MainActivity(734): getReason=apnChanged
	03-16 16:20:39.295: INFO/MainActivity(734): getSubtype=2
	03-16 16:20:39.295: INFO/MainActivity(734): getSubtypeName=EDGE
	03-16 16:20:39.295: INFO/MainActivity(734): getExtraInfo=cmwap
	03-16 16:20:39.295: INFO/MainActivity(734): getTypeName=mobile
	03-16 16:20:39.295: INFO/MainActivity(734): getType=0
	 */
	
	/*移动     net连接
	03-16 16:23:34.045: INFO/MainActivity(734): getDetailedState=CONNECTED
	03-16 16:23:34.045: INFO/MainActivity(734): getReason=apnSwitched
	03-16 16:23:34.045: INFO/MainActivity(734): getSubtype=2
	03-16 16:23:34.045: INFO/MainActivity(734): getSubtypeName=EDGE
	03-16 16:23:34.045: INFO/MainActivity(734): getExtraInfo=cmnet
	03-16 16:23:34.045: INFO/MainActivity(734): getTypeName=mobile
	03-16 16:23:34.045: INFO/MainActivity(734): getType=0

	/电信    
	05-25 18:55:30.180: INFO/MainActivity(731): getDetailedState=DISCONNECTED
	05-25 18:55:30.180: INFO/MainActivity(731): getReason=dataDisabled
	05-25 18:55:30.180: INFO/MainActivity(731): getSubtype=6
	05-25 18:55:30.180: INFO/MainActivity(731): getSubtypeName=CDMA - EvDo rev. A
	05-25 18:55:30.180: INFO/MainActivity(731): getExtraInfo=#777
	05-25 18:55:30.180: INFO/MainActivity(731): getTypeName=MOBILE
	05-25 18:55:30.180: INFO/MainActivity(731): getType=0
*/
}

你可能感兴趣的:(电信)