android:为每个设备获取或创建唯一ID

问题描述 投票:3回答:5

我正在使用api 14(android 4.0)开发一个应用程序。

在清单中:

 <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

我想从每个设备(或创建一个)获得一个唯一的ID,即使在重启设备后也可能是相同的。但重要的是,即使对于2个相同的设备,id也是不同的。我怎样才能做到这一点?

android uniqueidentifier
5个回答
3
投票

您可以将设备的IMEI号用作唯一ID。

你想打电话给android.telephony.TelephonyManager.getDeviceId()

这将返回唯一标识设备的字符串(GSM上的IMEI,CDMA的MEID)。

您需要AndroidManifest.xml中的以下权限:

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

1
投票

您可以使用GCM生成不同的设备令牌....即使您将卸载并再次安装应用程序或出厂设置后此设备令牌仍将保持相同...您必须按照一些步骤......创建一个新的Google Developers Console中的项目。在此步骤中,为简单起见,您只需要记下2个值:项目编号,它将在客户端项目中用作SENDER_ID;和API服务器密钥(在Credentials上创建),它将在服务器项目中用作API_KEY。为服务器端创建一个新的简单Android项目(基本源代码作为我在以下链接中的答案)。

为客户端创建一个新的简单Android项目(基本源代码作为我在以下链接中的答案,我从Google云消息传递的原始源 - GitHub进行了自定义)。

运行客户端应用程序,您将获得注册令牌(表示您的设备已成功注册)。然后,将此令牌粘贴(硬编码)到服务器应用程序中的CLIENT_REGISTRATION_TOKEN变量(或编写代码以将此令牌发送到服务器应用程序)。您可以在以下问题中阅读更多内容,其中一个问题是您之前阅读过的一个问题:

如何使用Android Studio为Android实现GCM Hello World为Android添加Google Cloud Messagin(GCM) - 注册过程


1
投票

试试这个String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);

这里android_id是每个设备的唯一ID。


1
投票

试试这个代码

 String UniqueDeviceId = AndroidDeviceIdentifier.getUniqueDeviceIdentifier(context);

也加入这个课程。

final class AndroidDeviceIdentifier {

private AndroidDeviceIdentifier() {
    // hidden constructor of singleton
}

/**
 * Returns a stable identifier for the current device.
 *
 * @param ctx The application's Context
 * @return The unique device identifier
 * @throws IllegalStateException If the device's identifier could not be determined
 */
public static String getUniqueDeviceIdentifier(@NonNull final Context ctx) throws IllegalStateException {
    try {
        return getDeviceUUID(ctx);
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
        throw new IllegalStateException("Could not determine device identifier", e);
    }
}

private static String getDeviceUUID(Context ctx) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] hash = makeHash(getMac(ctx), getSerialNumber(ctx));
    return createUUIDFromHash(hash);
}

private static String createUUIDFromHash(byte[] hash) {
    return UUID.nameUUIDFromBytes(hash).toString().toLowerCase(); // Server side wants lower cased UUIDs
}

private static byte[] makeHash(final String mac, final String serialNumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    MessageDigest sha;

    sha = MessageDigest.getInstance("SHA-256");
    sha.reset();

    sha.update(mac.getBytes("UTF-8"));
    sha.update(serialNumber.getBytes("UTF-8"));

    return sha.digest();
}

private static String getSerialNumber(Context context) {
    String serialNumber = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    if (serialNumber == null) {
        serialNumber = "0000000000000000";
    }

    return serialNumber;
}

private static String getMac(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String mac = wifiManager.getConnectionInfo().getMacAddress();
    if (mac == null) {
        mac = "000000000000";
    }
    return mac;
}

您将获得唯一的设备ID。如果你有任何问题,请告诉我


0
投票

尝试下面的代码,此设备ID是常量,即使您卸载应用程序并重新安装此ID仍保持不变,您也可以使用它来从数据库中检索用户数据。

final String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
            Settings.Secure.ANDROID_ID);

只需将此代码粘贴到主活动或任何功能中,即可存储在共享首选项中生成的ID以供日后使用。

© www.soinside.com 2019 - 2024. All rights reserved.