为了账号安全,请及时绑定邮箱和手机立即绑定

如何找到Android设备的序列号?

如何找到Android设备的序列号?

萧十郎 2019-07-08 16:26:57
如何找到Android设备的序列号?我需要为一个Android应用程序使用一个唯一的ID,我认为这个设备的序列号会是一个很好的选择。如何在我的应用程序中检索Android设备的序列号?
查看完整描述

3 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

正如DaveWebb提到的,Android开发者博客有一篇文章包括这件事。

我和谷歌的一个人谈过,想得到一些关于一些项目的更多的澄清。下面是我在上述博客文章中没有提到的发现:

  • Android_ID是首选的解决方案。Android_ID在Android<=2.1或>=2.3的版本上是完全可靠的。这篇文章中提到的问题只有2.2个。
  • 一些制造商的一些设备受到2.2中的Android_ID错误的影响。
  • 据我所知,所有受影响的设备

    相同的Android_ID

    ,也就是

    9774d56d682e549c

    ..它也是仿真器报告的相同的设备ID。
  • Google相信原始设备制造商已经为他们的许多或大多数设备修补了这个问题,但我能够证实,至少在2011年4月初,仍然很容易找到有坏Android_ID的设备。

根据Google的建议,我实现了一个类,它将为每个设备生成一个唯一的UUID,在适当情况下使用Android_ID作为种子,在必要时返回TelephonyManager.getDeviceId(),如果失败,则使用一个随机生成的惟一UUID,该UUID在应用程序重新启动(但不是app重新安装)中持久化。

import android.content.Context;import android.content.SharedPreferences;import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;import java.io.UnsupportedEncodingException;import java.util.UUID;public class DeviceUuidFactory {

    protected static final String PREFS_FILE = "device_id.xml";
    protected static final String PREFS_DEVICE_ID = "device_id";
    protected static volatile UUID uuid;

    public DeviceUuidFactory(Context context) {
        if (uuid == null) {
            synchronized (DeviceUuidFactory.class) {
                if (uuid == null) {
                    final SharedPreferences prefs = context                            
                    .getSharedPreferences(PREFS_FILE, 0);
                    final String id = prefs.getString(PREFS_DEVICE_ID, null);
                    if (id != null) {
                        // Use the ids previously computed and stored in the
                        // prefs file
                        uuid = UUID.fromString(id);
                    } else {
                        final String androidId = Secure.getString(
                            context.getContentResolver(), Secure.ANDROID_ID);
                        // Use the Android ID unless it's broken, in which case
                        // fallback on deviceId,
                        // unless it's not available, then fallback on a random
                        // number which we store to a prefs file
                        try {
                            if (!"9774d56d682e549c".equals(androidId)) {
                                uuid = UUID.nameUUIDFromBytes(androidId                                        
                                .getBytes("utf8"));
                            } else {
                                final String deviceId = ((TelephonyManager) 
                                        context.getSystemService(
                                            Context.TELEPHONY_SERVICE))
                                            .getDeviceId();
                                uuid = deviceId != null ? UUID                                        
                                .nameUUIDFromBytes(deviceId                                                
                                .getBytes("utf8")) : UUID                                        
                                .randomUUID();
                            }
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }
                        // Write the value out to the prefs file
                        prefs.edit()
                                .putString(PREFS_DEVICE_ID, uuid.toString())
                                .commit();
                    }
                }
            }
        }
    }

    /**
     * Returns a unique UUID for the current android device. As with all UUIDs,
     * this unique ID is "very highly likely" to be unique across all Android
     * devices. Much more so than ANDROID_ID is.
     * 
     * The UUID is generated by using ANDROID_ID as the base key if appropriate,
     * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
     * be incorrect, and finally falling back on a random UUID that's persisted
     * to SharedPreferences if getDeviceID() does not return a usable value.
     * 
     * In some rare circumstances, this ID may change. In particular, if the
     * device is factory reset a new device ID may be generated. In addition, if
     * a user upgrades their phone from certain buggy implementations of Android
     * 2.2 to a newer, non-buggy version of Android, the device ID may change.
     * Or, if a user uninstalls your app on a device that has neither a proper
     * Android ID nor a Device ID, this ID may change on reinstallation.
     * 
     * Note that if the code falls back on using TelephonyManager.getDeviceId(),
     * the resulting ID will NOT change after a factory reset. Something to be
     * aware of.
     * 
     * Works around a bug in Android 2.2 for many devices when using ANDROID_ID
     * directly.
     * 
     * @see http://code.google.com/p/android/issues/detail?id=10603
     * 
     * @return a UUID that may be used to uniquely identify your device for most
     *         purposes.
     */
    public UUID getDeviceUuid() {
        return uuid;
    }}


查看完整回答
反对 回复 2019-07-08
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

String serial = null; try {
    Class<?> c = Class.forName("android.os.SystemProperties");
    Method get = c.getMethod("get", String.class);
    serial = (String) get.invoke(c, "ro.serialno");} catch (Exception ignored) {}

此代码使用隐藏的AndroidAPI返回设备序列号。


查看完整回答
反对 回复 2019-07-08
  • 3 回答
  • 0 关注
  • 877 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信