2016-05-11 8 views
0

デュアルsimのデバイスのデバイスIDを取得できません。デュアルSIMスロットのデバイス(< 5.1 os)のデバイスはnullです。デバイスIDを取得すると、デュアルSIMを搭載したデバイスがnullになります。

は、以下のデバイスID

public static String getImeiNumber(Context context) { 
    try { 
     if (context != null) { 
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      if (telephonyManager != null) { 
       Log.d(TAG, "Device ID" + telephonyManager.getDeviceId()); 
       return telephonyManager.getDeviceId(); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

ここで私はtelephony managerの助けを借りて、デバイスIDを取得していますに取得するために私のコードです。

+0

に入力を与える必要があるかもしれません。 – Dhruv

答えて

0

menifestファイルでこの権限を使用します。

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

このコードを試してみてください

public String getIMEI(Context context){ 
     String imei=" "; 
     try{ 
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      imei = telephonyManager.getDeviceId(); 
     }catch (Exception ex){ 
      Log.e(CommonContents.TAG, ex.getMessage()); 
     } 
     return imei; 
    } 

編集

例えば、サブスクリプションの一意のデバイスIDを返し

APIレベルで23

を追加 getDeviceId(int slotId)、使用してみてくださいGSM用のIMEIおよびCDMA電話用のMEID。デバイスIDが使用できない場合はnullを返します。

あなたがセキュアIDを使用することができ、デバイスの一意のIDが必要な場合我々は、各SIMカード用

Log.i("OmSai ", "Single or Dula Sim "+telephonyManager.getPhoneCount()); 
Log.i("OmSai ", "Defualt device ID "+telephonyManager.getDeviceId()); 
Log.i("OmSai ", "Single 1 "+telephonyManager.getDeviceId(0)); 
Log.i("OmSai ", "Single 2 "+telephonyManager.getDeviceId(1)); 

をAndroidのAPIとIMEIを使用してかどうか、電話シングルまたはデュアルSIMを確認することができます。これは、IMEIを取得するには、このクラスとそのメソッドを試してみても

public String getDeviceID(Context context){ 
     String deviceID = " "; 
     try{ 
      deviceID = Settings.Secure.getString(context.getContentResolver(), 
        Settings.Secure.ANDROID_ID); 
     }catch (Exception ex){ 
      Log.e(CommonContents.TAG, ex.getMessage()); 
     } 
     return deviceID; 
    } 
+0

私は許可を使用しているのと同じです。問題は<5.1デバイスである –

+0

どのような目的のためにデバイスIDが必要ですか?各デバイスに固有のキーが必要ですか? – Pehlaj

+0

そのデバイスIDを送信する必要があります私はアンドロイドIDまたは他のIDを使用することができますが、要件IDデバイスID –

0

ユニークです。 getInstanceメソッドはTelephonyInfoインスタンスを返します。異なるデバイスの場合

、私たちは、私は4.4を有するデバイス上のあなたのコードをテストしている、それが正常に動作している方法を推測し、getDeviceIdBySlot方法

public class TelephonyInfo { 

    private static TelephonyInfo telephonyInfo; 
    private String imsiSIM1; 
    private String imsiSIM2; 
    private boolean isSIM1Ready; 
    private boolean isSIM2Ready; 

    public String getImsiSIM1() { 
     return imsiSIM1; 
    } 

    /*public static void setImsiSIM1(String imsiSIM1) { 
     TelephonyInfo.imsiSIM1 = imsiSIM1; 
    }*/ 

    public String getImsiSIM2() { 
     return imsiSIM2; 
    } 

    /*public static void setImsiSIM2(String imsiSIM2) { 
     TelephonyInfo.imsiSIM2 = imsiSIM2; 
    }*/ 

    public boolean isSIM1Ready() { 
     return isSIM1Ready; 
    } 

    /*public static void setSIM1Ready(boolean isSIM1Ready) { 
     TelephonyInfo.isSIM1Ready = isSIM1Ready; 
    }*/ 

    public boolean isSIM2Ready() { 
     return isSIM2Ready; 
    } 

    /*public static void setSIM2Ready(boolean isSIM2Ready) { 
     TelephonyInfo.isSIM2Ready = isSIM2Ready; 
    }*/ 

    public boolean isDualSIM() { 
     return imsiSIM2 != null; 
    } 

    private TelephonyInfo() { 
    } 

    public static TelephonyInfo getInstance(Context context){ 

     if(telephonyInfo == null) { 

      telephonyInfo = new TelephonyInfo(); 

      TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); 


      telephonyInfo.imsiSIM1 = telephonyManager.getDeviceId();; 
      telephonyInfo.imsiSIM2 = null; 

      try { 
       telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0); 
       telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1); 
      } catch (GeminiMethodNotFoundException e) { 
       Log.e(BLL_LOG,"First GeminiMethodNotFoundException Ex="+e.toString()); 

       try { 
        telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0); 
        telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1); 
       } catch (GeminiMethodNotFoundException e1) { 
        //Call here for next manufacturer's predicted method name if you wish 
        Log.e(BLL_LOG, "Second GeminiMethodNotFoundException Ex=" + e1.toString()); 
       } 
      } 

      telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY; 
      telephonyInfo.isSIM2Ready = false; 

      try { 
       telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0); 
       telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1); 
      } catch (GeminiMethodNotFoundException e) { 

       e.printStackTrace(); 

       try { 
        telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0); 
        telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1); 
       } catch (GeminiMethodNotFoundException e1) { 
        //Call here for next manufacturer's predicted method name if you wish 
        e1.printStackTrace(); 
       } 
      } 
     } 

     return telephonyInfo; 
    } 

    private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException { 

     String imsi = null; 

     TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 

     try{ 

      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){ 
       imsi = ob_phone.toString(); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new GeminiMethodNotFoundException(predictedMethodName); 
     } 

     return imsi; 
    } 

    private static boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException { 

     boolean isReady = false; 

     TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 

     try{ 

      Class<?> telephonyClass = Class.forName(telephony.getClass().getName()); 

      Class<?>[] parameter = new Class[1]; 
      parameter[0] = int.class; 
      Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter); 

      Object[] obParameter = new Object[1]; 
      obParameter[0] = slotID; 
      Object ob_phone = getSimStateGemini.invoke(telephony, obParameter); 

      if(ob_phone != null){ 
       int simState = Integer.parseInt(ob_phone.toString()); 
       if(simState == TelephonyManager.SIM_STATE_READY){ 
        isReady = true; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new GeminiMethodNotFoundException(predictedMethodName); 
     } 

     return isReady; 
    } 


    private static class GeminiMethodNotFoundException extends Exception { 

     private static final long serialVersionUID = -996812356902545308L; 

     public GeminiMethodNotFoundException(String info) { 
      super(info); 
     } 
    } 
} 
+0

私はこの方法で試しましたが、 "getDeviceIdGemini"がすべてのデバイスで動作するかどうかはわかりませんが、他のデバイスでは破損する可能性があります –

+0

合意しました! getDeviceIdBySlotを使用して対応するメソッドを取得する必要があります – Stallion

関連する問題