Android 安卓唯一ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3115918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android unique id
提问by jaimin
How do I get an unique ID from an Android phone?
如何从 Android 手机获取唯一 ID?
Whenever I try to get the unique ID from the phone as a string it always shows android idand no other unique hex values.
每当我尝试从手机中获取唯一 ID 作为字符串时,它总是显示android id而没有其他唯一的十六进制值。
How do I get that one?
我怎么得到那个?
This is the code I use to get the ID until now:
这是迄今为止我用来获取 ID 的代码:
String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);
the output which I get looks like this:
我得到的输出如下所示:
Android id is: android id
I am using a Nexus One for testing.
我正在使用 Nexus One 进行测试。
回答by Kevin Parker
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:
有关如何为安装应用程序的每个 Android 设备获取唯一标识符的详细说明,请参阅此官方 Android 开发人员博客帖子:
http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
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.
我个人认为这可以接受但并不理想。Android 提供的任何标识符都不能在所有情况下都有效,因为大多数标识符取决于手机的无线电状态(wifi 开/关、蜂窝网络开/关、蓝牙开/关)。其他如 Settings.Secure.ANDROID_ID 必须由制造商实施,并且不保证是唯一的。
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.
以下是将数据写入 INSTALLATION 文件的示例,该文件将与应用程序在本地保存的任何其他数据一起存储。
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();
}
}
回答by drawnonward
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
with manifest
有清单
<uses-permission android:name='android.permission.READ_PHONE_STATE' />
Edit:
编辑:
Here is some interesting reading about the android id:
这里有一些关于 android id 的有趣读物:
Android ID Requires Market Login
Try setting it to something other than 'android id' and see if you read the new value.
尝试将其设置为“android id”以外的其他内容,看看您是否读取了新值。
回答by sns
Here is code segment how to get androidId, unique DeviceId and Serial Number for your android phone may be it helps you.
以下是如何为您的 android 手机获取 androidId、唯一 DeviceId 和序列号的代码段可能对您有所帮助。
TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String DeviceId, SerialNum, androidId;
DeviceId = tm.getDeviceId();
SerialNum = tm.getSimSerialNumber();
androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
String mydeviceId = deviceUuid.toString();
Log.v("My Id", "Android DeviceId is: " +DeviceId);
Log.v("My Id", "Android SerialNum is: " +SerialNum);
Log.v("My Id", "Android androidId is: " +androidId);
回答by ognian
回答by Sreedev R
Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
this is not a good method it will return null in some cases.
这不是一个好方法,它在某些情况下会返回 null。
THis piece of code will help you to generate unique pseudodevice ID .......``
这段代码将帮助您生成唯一的伪设备 ID ......``
public String getDeviceID() {
/*String Return_DeviceID = USERNAME_and_PASSWORD.getString(DeviceID_key,"Guest");
return Return_DeviceID;*/
TelephonyManager TelephonyMgr = (TelephonyManager) getApplicationContext().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String m_szImei = TelephonyMgr.getDeviceId(); // Requires
// READ_PHONE_STATE
// 2 compute DEVICE ID
String m_szDevIDShort = "35"
+ // we make this look like a valid IMEI
Build.BOARD.length() % 10 + Build.BRAND.length() % 10
+ Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
+ Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
+ Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
+ Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
+ Build.TAGS.length() % 10 + Build.TYPE.length() % 10
+ Build.USER.length() % 10; // 13 digits
// 3 android ID - unreliable
String m_szAndroidID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
// 4 wifi manager, read MAC address - requires
// android.permission.ACCESS_WIFI_STATE or comes as null
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
// 5 Bluetooth MAC address android.permission.BLUETOOTH required
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
System.out.println("m_szBTMAC "+m_szBTMAC);
// 6 SUM THE IDs
String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
System.out.println("m_szLongID "+m_szLongID);
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
byte p_md5Data[] = m.digest();
String m_szUniqueID = new String();
for (int i = 0; i < p_md5Data.length; i++) {
int b = (0xFF & p_md5Data[i]);
// if it is a single digit, make sure it have 0 in front (proper
// padding)
if (b <= 0xF)
m_szUniqueID += "0";
// add number to string
m_szUniqueID += Integer.toHexString(b);
}
m_szUniqueID = m_szUniqueID.toUpperCase();
Log.i("-------------DeviceID------------", m_szUniqueID);
Log.d("DeviceIdCheck", "DeviceId that generated MPreferenceActivity:"+m_szUniqueID);
return m_szUniqueID;
}
回答by Fahim
Unique Id
唯一身份
Info adInfo = null;
?
try {
???? adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
???? ...
} catch (GooglePlayServicesAvailabilityException e) {
???? ...
} catch (GooglePlayServicesNotAvailableException e) {
???? ...
}
?
String AdId = adInfo.getId();
?
?