Android携帯機器情報の取得:機種、オペレーティングシステムのバージョン番号、携帯電話の解像度、キャリア、現在のネットワーク方式、IMEI、MEID、MACアドレスを含む

11846 ワード

Android携帯機器情報の取得:オペレーティングシステムのバージョン番号、携帯電話の解像度、キャリア、現在のネットワーク方式、IMEI、MEID、MACアドレス
デュアルカードモデルの2つのIMEI情報を含む

/**
 * Created by Administrator on 2019/5/15.
 *       
 */

public class DeviceInfoModel {

    private static final String TAG = "DeviceInfoModel";
    private static DeviceInfoModel instance;

    public DeviceInfoModel() {
    }


    public static DeviceInfoModel getInstance() {

        if (instance == null) {
            instance = new DeviceInfoModel();
        }
        return instance;
    }

    /**
     *     
     */
    public String getPhoneModel() {
//        TelephonyManager manager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        String mtype = android.os.Build.MODEL;
        String brand = android.os.Build.BRAND;//    
        String model = android.os.Build.MODEL;//    
        Log.w(TAG, "    :" + brand + " " + model);
        return brand + " " + model;
    }

    /**
     *       
     *
     * @return
     */
    public String getOS() {
        Log.w(TAG, "    :" + "Android" + android.os.Build.VERSION.RELEASE);
        return "Android" + android.os.Build.VERSION.RELEASE;
    }


    /**
     *        
     *
     * @param context
     * @return
     */
    public String getResolution(Context context) {
        //   1 Android        
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        Log.w(TAG, "   :" + screenWidth + "*" + screenHeight);
        return screenWidth + "*" + screenHeight;

    }


    /**
     *        
     */
    public String getDeviceNo(Context context) {
        if (!checkReadPhoneStatePermission(context)) {
            Log.w(TAG, "       :    ");
            return "";
        }
        TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
//        @SuppressLint("HardwareIds") String szImei = TelephonyMgr.getDeviceId();

        Method method = null;
        String imei2 = "";
        try {
            method = TelephonyMgr.getClass().getMethod("getDeviceId", int.class);
            String imei1 = TelephonyMgr.getDeviceId();
            String imei0 = (String) method.invoke(TelephonyMgr, 0);
            imei2 = (String) method.invoke(TelephonyMgr, 1);
            String meid = (String) method.invoke(TelephonyMgr, 2);

            Log.e(TAG, "     szImei-0 is  :" + imei0 + "  ---  imei1: " + imei1 + "  ---  imei2: " + imei2 + "   -meid is :" + meid);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            Log.w(TAG, "     imei-NoSuchMethodException: " + e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.w(TAG, "     imei-IllegalAccessException: " + e.getMessage());
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            Log.w(TAG, "     imei-InvocationTargetException: " + e.getMessage());
        }
        return imei2;
    }

    /**
     *      
     *
     * @param context
     * @return
     */
    public String getNetOperator(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String iNumeric = manager.getSimOperator();
        String netOperator = "";
        if (iNumeric.length() > 0) {
            if (iNumeric.equals("46000") || iNumeric.equals("46002")) {
                //     
                netOperator = "    ";
            } else if (iNumeric.equals("46003")) {
                //     
                netOperator = "    ";
            } else if (iNumeric.equals("46001")) {
                //     
                netOperator = "    ";
            } else {
                //  
                netOperator = "  ";
            }
        }
        Log.w(TAG, "   :" + netOperator);
        return netOperator;
    }


    /**
     *       
     */
    public String getNetMode(Context context) {
        String strNetworkType = "  ";
//        TelephonyManager manager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        manager.getNetworkType();
        ConnectivityManager manager = (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            int netMode = networkInfo.getType();
            if (netMode == ConnectivityManager.TYPE_WIFI) {
                strNetworkType = "WIFI";
                //wifi
            } else if (netMode == ConnectivityManager.TYPE_MOBILE) {
                int networkType = networkInfo.getSubtype();
                switch (networkType) {

                    //2g
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                        strNetworkType = "2G";
                        break;

                    //3g
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                        strNetworkType = "3G";
                        break;

                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                        strNetworkType = "4G";
                        break;

                    default:
                        String _strSubTypeName = networkInfo.getSubtypeName();
                        // http://baike.baidu.com/item/TD-SCDMA              3G  
                        if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                            strNetworkType = "3G";
                        } else {
                            strNetworkType = _strSubTypeName;
                        }
                        break;
                }
            }
        }
        Log.w(TAG, "    :" + strNetworkType);
        return strNetworkType;
    }

    private boolean checkReadPhoneStatePermission(Context context) {
        try {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE},
                        10);
                return false;
            }
        } catch (IllegalArgumentException e) {
            return false;
        }
        return true;
    }


    @SuppressLint({"MissingPermission", "HardwareIds"})
    public String getMEID(Context context) {
        if (!checkReadPhoneStatePermission(context)) {
            Log.w(TAG, "       -getMEID:    ");
            return "";
        }
        String meid = "";
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != mTelephonyMgr) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                meid = mTelephonyMgr.getMeid();
                Log.i(TAG, "Android    o-26-      ---meid:" + meid);
            } else {
                meid = mTelephonyMgr.getDeviceId();
            }
        }

        Log.i(TAG, "      ---meid:" + meid);

        return meid;
    }

    @SuppressLint("MissingPermission")
    public String getIMEI(Context context) {
        if (!checkReadPhoneStatePermission(context)) {
            Log.w(TAG, "       -getIMEI:    ");
            return "";
        }
        String imei1 = "";
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != mTelephonyMgr) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei1 = mTelephonyMgr.getImei(0);
                Log.i(TAG, "Android    o-26-      ---imei-1:" + imei1);
            } else {
                try {
                    imei1 = getDoubleImei(mTelephonyMgr, "getDeviceIdGemini", 0);
                } catch (Exception e) {
                    try {
                        imei1 = getDoubleImei(mTelephonyMgr, "getDeviceId", 0);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    Log.e(TAG, "get device id fail: " + e.toString());
                }
            }
        }

        Log.i(TAG, "      ---imei1:" + imei1);
        return imei1;
    }

    @SuppressLint("MissingPermission")
    public String getIMEI2(Context context) {
        if (!checkReadPhoneStatePermission(context)) {
            Log.w(TAG, "       -getIMEI2:    ");
            return "";
        }
        String imei2 = "";
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != mTelephonyMgr) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei2 = mTelephonyMgr.getImei(1);
                mTelephonyMgr.getMeid();
                Log.i(TAG, "Android    o-26-      ---imei-2:" + imei2);
            } else {
                try {
                    imei2 = getDoubleImei(mTelephonyMgr, "getDeviceIdGemini", 1);
                } catch (Exception e) {
                    try {
                        imei2 = getDoubleImei(mTelephonyMgr, "getDeviceId", 1);
                    } catch (Exception ex) {
                        Log.e(TAG, "get device id fail: " + e.toString());
                    }
                }
            }
        }

        Log.i(TAG, "      --- imei2:" + imei2);
        return imei2;
    }

    /**
     *        imei
     */
    private String getDoubleImei(TelephonyManager telephony, String predictedMethodName, int slotID) throws Exception {
        String inumeric = null;

        Class> telephonyClass = Class.forName(telephony.getClass().getName());
        Class>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);
        if (ob_phone != null) {
            inumeric = ob_phone.toString();
        }
        return inumeric;
    }
}