Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (C) 2014 MUSE team Inria Paris - Rocquencourt * * This file is part of UCNDataCollector. * * UCNDataCollector is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * UCNDataCollector is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero Public License for more details. * * You should have received a copy of the GNU Affero Public License * along with UCNDataCollector. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import java.io.UnsupportedEncodingException; import java.util.UUID; import android.content.Context; import android.content.SharedPreferences; import android.provider.Settings.Secure; import android.telephony.TelephonyManager; public class Main { private static final String ID_PREFS_FILE = "ucn_device_id.xml"; private static final String ID_PREFS_DEVICE_ID = "ucn_device_id"; private static final String BAD_UUID = "9774d56d682e549c"; private static UUID getUuid(Context c) { final SharedPreferences prefs = c.getSharedPreferences(ID_PREFS_FILE, Context.MODE_PRIVATE); final String id = prefs.getString(ID_PREFS_DEVICE_ID, null); final UUID uuid; if (id != null) { uuid = UUID.fromString(id); } else { final String androidId = Secure.getString(c.getContentResolver(), Secure.ANDROID_ID); try { if (!BAD_UUID.equals(androidId)) { uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); } else { final String deviceId = ((TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE)) .getDeviceId(); if (deviceId != null) uuid = UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")); else uuid = UUID.randomUUID(); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } prefs.edit().putString(ID_PREFS_DEVICE_ID, uuid.toString()).commit(); } return uuid; } }