如何从Android手机中获得唯一的ID?
每当我试图以字符串的形式从电话中获取唯一的ID时,它总是显示android id,而没有其他唯一的十六进制值。
我怎么弄到那个?
到目前为止,这是我用来获取ID的代码:
String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);我得到的输出如下:
Android id is: android id我正在用Nexus进行测试。
发布于 2011-08-02 03:17:36
有关如何为安装应用程序的每个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();
}
}发布于 2010-06-25 06:11:09
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();带着清单
<uses-permission android:name='android.permission.READ_PHONE_STATE' />编辑:
下面是关于android id的一些有趣的文章:
如何设置Android ID
Android ID需要市场登录
尝试将其设置为“android”以外的其他内容,并查看是否读取了新值。
发布于 2011-07-21 13:01:30
下面是代码段,如何获得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); https://stackoverflow.com/questions/3115918
复制相似问题