码迷,mamicode.com
首页 > 移动开发 > 详细

两种方式判断移动运营商

时间:2016-08-10 14:06:13      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * @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" +//直接获取移动运营商名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        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
*/
}

转载:http://www.cnblogs.com/stay/articles/2057948.html

两种方式判断移动运营商

标签:

原文地址:http://www.cnblogs.com/cyqx/p/5756227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!