【问题标题】:Find unique name of device?查找设备的唯一名称?
【发布时间】:2011-06-01 21:06:52
【问题描述】:

我正在开发一个应用程序,它使用 zeroconf (bonjour) 来发现设备 - 所以我需要给每个 android 设备起一个名字(不仅仅是一堆数字和字母,而是像“Alex's Device”这样有意义的名字) .在 iOS 中可以轻松完成 - 在 android 中可以吗?

【问题讨论】:

  • @inazaruk 我不相信是骗子:使用 64 位十六进制字符串或 mac 地址不是我所说的像“Alex 的设备”这样有意义的东西)
  • 只需通过字段或首选项向用户询问名称。
  • @CommonsWare 这显然是最好的解决方案,但提供一些用户友好的默认值总是很好
  • @Aleadam:好的,现在我想我更好地理解了最初的问题。
  • @Aleadam,你是对的,我的错。

标签: android networking bonjour zeroconf


【解决方案1】:

可以有许多帐户链接到该设备。您可以使用AccountManager 来获取它们。例如,Google 帐户上的电子邮件:

AccountManager am = AccountManager.get(this);

Account[] ac = am.getAccountsByType("com.google");

for (Account account : ac) {
  Log.d ("Account", ac.name);
}

或者,您可以使用android.os.Build.MODEL 或类似名称。

【讨论】:

  • 账户名不是唯一的。我可以将两台设备关联到同一个 Google 帐户。
  • 这样您就可以拥有两部具有相同用户名的iphone。我试图给出更接近问题的答案。
  • 他不是这么问的。 “所以我需要给每个 android 设备”。每个 ... 设备,而不是每个 ... 用户。
  • @farble “这不是他问的”是什么意思?如果 OP 接受了答案,那肯定是有原因的...我尝试阅读问题的 context,而不仅仅是单词。
  • 让我再试一次,引用 OP:“我需要给每个 android 设备起一个名字”。请注意,他说他需要给“每个设备”,而不是用户。帐户名是用户名,它们不是 OP 要求的每个设备。现在知道了吗?
【解决方案2】:

假设您的应用程序需要设备具有网络连接,您可以尝试使用设备的 MAC 地址,该地址应该是全球唯一的。这可以通过 WifiInfo 类获得:

WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String macAddress = info.getMacAddress();

您还需要在清单中设置 ACCESS_WIFI_STATE 权限。

【讨论】:

    【解决方案3】:

    你可以得到Android手机的IMEI,它是唯一的,

    可以通过TelephonyManager类的getDeviceId()方法获取

    注意:您需要在清单中添加权限:READ_PHONE_STATE

    【讨论】:

    • 我说的是 Android 手机,我没有说平板电脑,我认为在平板电脑上,有一个标识符,我没有工作或使用它,只是“我认为”:)跨度>
    【解决方案4】:

    有关如何为安装您的应用程序的每台 Android 设备获取唯一标识符的详细说明,请参阅此官方 Android 开发者博客帖子:

    http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

    似乎最好的方法是您在安装时自己生成一个,然后在重新启动应用程序时阅读它。

    我个人认为这是可以接受的,但并不理想。 Android 提供的标识符在所有情况下都不起作用,因为大多数标识符取决于手机的无线电状态(wifi 开/关、蜂窝开/关、蓝牙开/关)。其他如 Settings.Secure.ANDROID_ID 必须由制造商实现,不保证唯一。

    以下是将数据写入安装文件的示例,该文件将与应用程序在本地保存的任何其他数据一起存储。

    public class Installation {
        private static String sID = null;
        private static final String INSTALLATION = "INSTALLATION";
    
        public synchronized static String id(Context context) {
            if (sID == null) {  
                File installation = new File(context.getFilesDir(), INSTALLATION);
                try {
                    if (!installation.exists())
                        writeInstallationFile(installation);
                    sID = readInstallationFile(installation);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            return sID;
        }
    
        private static String readInstallationFile(File installation) throws IOException {
            RandomAccessFile f = new RandomAccessFile(installation, "r");
            byte[] bytes = new byte[(int) f.length()];
            f.readFully(bytes);
            f.close();
            return new String(bytes);
        }
    
        private static void writeInstallationFile(File installation) throws IOException {
            FileOutputStream out = new FileOutputStream(installation);
            String id = UUID.randomUUID().toString();
            out.write(id.getBytes());
            out.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      相关资源
      最近更新 更多