找到设备的唯一名称?

发布于 2024-11-13 08:06:41 字数 147 浏览 2 评论 0原文

我正在开发一个使用 Zeroconf (bonjour) 来发现设备的应用程序 - 因此我需要为每个 Android 设备提供某种名称(不仅仅是一堆数字和字母,而是一些有意义的名称,例如“Alex 的设备”)。在 iOS 中这可以很容易地完成 - 这在 Android 中可能吗?

I'm developing an app that uses zeroconf (bonjour) to discover devices - so I need to give each android device some kind of name (not just a bunch of numbers and letters, but something meaningful like "Alex's Device"). In iOS it can be easily done - is this possible in android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁憨憨 2024-11-20 08:06:41

可以有多个帐户链接到该设备。您可以使用 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。模型或类似的。

There can be many accounts linked to the device. You can use the AccountManager to get them. For example, emails on google accounts:

AccountManager am = AccountManager.get(this);

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

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

Alternatively, you can use android.os.Build.MODEL or similar.

日记撕了你也走了 2024-11-20 08:06:41

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

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

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

Assuming your application requires a device to have a network connection, you can try using the MAC address of the device, which should be globally unique. This can be obtained with the WifiInfo class:

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

You'll also need to set the ACCESS_WIFI_STATE permission in your manifest.

2024-11-20 08:06:41

你可以获取Android手机的IMEI,它是唯一的,

你可以使用TelephonyManager类的方法getDeviceId()获取它

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

you can get the IMEI of the Android Phone , it's Unique ,

you can Get it by using the method getDeviceId() of the Class TelephonyManager

NOTE : you will need to add a permission in your manifest : READ_PHONE_STATE

悲凉≈ 2024-11-20 08:06:41

有关如何为安装应用程序的每个 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();
    }
}

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:

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

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.

I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文