首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得DeviceID安卓系统?

如何获得DeviceID安卓系统?
EN

Stack Overflow用户
提问于 2013-08-22 14:17:44
回答 3查看 5.4K关注 0票数 2

如何在安卓系统中获得单一的设备in?智能手机和平板电脑。据我所知,这可能不是单一的吗?

这段代码能工作吗?http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-22 14:27:50

这是获取同样适用于平板电脑的设备Id的简单方法。

代码语言:javascript
复制
public class DeviceHelper {

private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";

public synchronized static String getDeviceId(final Context context) {
    if (sID == null) {
        File installation = new File(context.getFilesDir(), INSTALLATION);
        try {
            if (!installation.exists()){
                writeInstallationFile(context,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(final Context context,File installation) throws IOException {
    FileOutputStream out = new FileOutputStream(installation);
    StringBuffer buf=new StringBuffer();
    buf.append(Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID));
    buf.append("-");
    buf.append(UUID.randomUUID().toString());
    out.write(buf.toString().getBytes());
    out.close();
}

}
票数 2
EN

Stack Overflow用户

发布于 2013-08-22 14:19:37

获得android设备唯一ID的最简单代码:(这也适用于平板电脑)。

代码语言:javascript
复制
    String deviceId = Secure.getString(MainActivity.this.getContentResolver(),Secure.ANDROID_ID);
    Log.d("Device ID",deviceId);
票数 0
EN

Stack Overflow用户

发布于 2013-08-22 14:26:45

另一种可能性是:

代码语言:javascript
复制
final TelephonyManager tm = (TelephonyManager) c
        .getSystemService(Context.TELEPHONY_SERVICE);
Log.w("ID", tm.getDeviceId());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18383053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档