【问题标题】:How can I get the deviceId of my Android emulator?如何获取我的 Android 模拟器的 deviceId?
【发布时间】:2012-02-04 21:18:11
【问题描述】:

我正在开发一个能够获得推送通知的安卓应用。但是我需要一个 deviceId 才能使其成功,并且由于我没有任何 android 手机,所以我曾经在模拟器中测试该应用程序。所以我的问题是,我可以为我的模拟器获取一个 deviceId。

【问题讨论】:

标签: android emulation


【解决方案1】:

获取模拟器的设备ID >>>

onCreate() 方法中添加这两行:

 String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
String deviceId = md5(android_id).toUpperCase();
Log.i("device id=",deviceId);

onCreate() 方法之外添加这个md5() 方法:

public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

要查找设备 ID,只需在您的 Android Studio 中运行应用并打开 logcat,然后点击搜索栏中的“设备 ID”

【讨论】:

    【解决方案2】:

    通过 Android 模拟器:

    1. 点击侧边菜单底部的“更多”点。
    2. 转到左侧导航面板底部的“帮助”部分。
    3. 在“帮助”中转到“关于”选项卡。
    4. 您可以在此处找到序列号。在某些情况下称为“deviceId”。

    【讨论】:

      【解决方案3】:

      这对我有用

      public static String getIMEI() {
          String IMEI = Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.ANDROID_ID);
          return IMEI;
      }
      

      【讨论】:

        【解决方案4】:

        使用此方法,此方法适用于平板电脑和手机两者

           public  String getDeviceID(Context context) {
                        TelephonyManager manager = 
                            (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                        String deviceId;
                        if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
                            //Tablet
                             deviceId = Secure.getString(this.getContentResolver(),
                                    Secure.ANDROID_ID);
        
                        } else {
                            //Mobile
                             deviceId = manager.getDeviceId();
        
                        }
                        return deviceId;
                    }
        

        【讨论】:

          【解决方案5】:

          您无法在 android 中获取设备 ID,但您可以获取 IMEI 号码以进行推送通知。 bcoz 所有设备都有不同的 IMEI 号码。在模拟器中,默认情况下您会获得 0000000000000 作为您的 IMEI,但在设备中您会获得完美的数字。下面是获取IMEI号码的代码

          TelephonyManager telephonyManager1 = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
              String imei = telephonyManager1.getDeviceId();
          

          【讨论】:

          • 问我是否还有任何疑问
          • 但考虑到没有移动数据连接的平板电脑没有 IMEI
          【解决方案6】:

          “adb devices”命令还列出了活动的模拟器,可以提供设备 ID。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-26
            • 2019-04-07
            • 2019-05-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-18
            相关资源
            最近更新 更多