Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.provider.Settings.Secure;
- private String android_id = Secure.getString(getContext().getContentResolver(),
- Secure.ANDROID_ID);
- final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
- final String tmDevice, tmSerial, androidId;
- tmDevice = "" + tm.getDeviceId();
- tmSerial = "" + tm.getSimSerialNumber();
- androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
- UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
- String deviceId = deviceUuid.toString();
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.provider.Settings.Secure;
- import android.telephony.TelephonyManager;
- import java.io.UnsupportedEncodingException;
- import java.util.UUID;
- public class DeviceUuidFactory {
- protected static final String PREFS_FILE = "device_id.xml";
- protected static final String PREFS_DEVICE_ID = "device_id";
- protected volatile static UUID uuid;
- public DeviceUuidFactory(Context context) {
- if (uuid == null) {
- synchronized (DeviceUuidFactory.class) {
- if (uuid == null) {
- final SharedPreferences prefs = context
- .getSharedPreferences(PREFS_FILE, 0);
- final String id = prefs.getString(PREFS_DEVICE_ID, null);
- if (id != null) {
- // Use the ids previously computed and stored in the
- // prefs file
- uuid = UUID.fromString(id);
- } else {
- final String androidId = Secure.getString(
- context.getContentResolver(), Secure.ANDROID_ID);
- // Use the Android ID unless it's broken, in which case
- // fallback on deviceId,
- // unless it's not available, then fallback on a random
- // number which we store to a prefs file
- try {
- if (!"9774d56d682e549c".equals(androidId)) {
- uuid = UUID.nameUUIDFromBytes(androidId
- .getBytes("utf8"));
- } else {
- final String deviceId = (
- (TelephonyManager) context
- .getSystemService(Context.TELEPHONY_SERVICE))
- .getDeviceId();
- uuid = deviceId != null ? UUID
- .nameUUIDFromBytes(deviceId
- .getBytes("utf8")) : UUID
- .randomUUID();
- }
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
- // Write the value out to the prefs file
- prefs.edit()
- .putString(PREFS_DEVICE_ID, uuid.toString())
- .commit();
- }
- }
- }
- }
- }
- /**
- * Returns a unique UUID for the current android device. As with all UUIDs,
- * this unique ID is "very highly likely" to be unique across all Android
- * devices. Much more so than ANDROID_ID is.
- *
- * The UUID is generated by using ANDROID_ID as the base key if appropriate,
- * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
- * be incorrect, and finally falling back on a random UUID that's persisted
- * to SharedPreferences if getDeviceID() does not return a usable value.
- *
- * In some rare circumstances, this ID may change. In particular, if the
- * device is factory reset a new device ID may be generated. In addition, if
- * a user upgrades their phone from certain buggy implementations of Android
- * 2.2 to a newer, non-buggy version of Android, the device ID may change.
- * Or, if a user uninstalls your app on a device that has neither a proper
- * Android ID nor a Device ID, this ID may change on reinstallation.
- *
- * Note that if the code falls back on using TelephonyManager.getDeviceId(),
- * the resulting ID will NOT change after a factory reset. Something to be
- * aware of.
- *
- * Works around a bug in Android 2.2 for many devices when using ANDROID_ID
- * directly.
- *
- * @see http://code.google.com/p/android/issues/detail?id=10603
- *
- * @return a UUID that may be used to uniquely identify your device for most
- * purposes.
- */
- public UUID getDeviceUuid() {
- return uuid;
- }
- }
- private static String uniqueID = null;
- private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
- public synchronized static String id(Context context) {
- if (uniqueID == null) {
- SharedPreferences sharedPrefs = context.getSharedPreferences(
- PREF_UNIQUE_ID, Context.MODE_PRIVATE);
- uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
- if (uniqueID == null) {
- uniqueID = UUID.randomUUID().toString();
- Editor editor = sharedPrefs.edit();
- editor.putString(PREF_UNIQUE_ID, uniqueID);
- editor.commit();
- }
- }
- return uniqueID;
- }
- WifiManager wm = (WifiManager)Ctxt.getSystemService(Context.WIFI_SERVICE);
- return wm.getConnectionInfo().getMacAddress();
- String serial = null;
- try {
- Class<?> c = Class.forName("android.os.SystemProperties");
- Method get = c.getMethod("get", String.class);
- serial = (String) get.invoke(c, "ro.serialno");
- } catch (Exception ignored) {
- }
- 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
- 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();
- }
- }
- String deviceId = Settings.System.getString(getContentResolver(),Settings.System.ANDROID_ID);
- <application android:label="MyApplication"
- android:backupAgent="MyBackupAgent">
- ...
- <meta-data android:name="com.google.android.backup.api_key"
- android:value="your_backup_service_key" />
- </application>
- public class MyBackupAgent extends BackupAgentHelper {
- // The name of the SharedPreferences file
- static final String PREFS = "user_preferences";
- // A key to uniquely identify the set of backup data
- static final String PREFS_BACKUP_KEY = "prefs";
- // Allocate a helper and add it to the backup agent
- @Override
- public void onCreate() {
- SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
- addHelper(PREFS_BACKUP_KEY, helper);
- }
- }
- BackupManager backupManager = new BackupManager(context);
- public static String getUserID(Context context) {
- private static String uniqueID = null;
- private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
- if (uniqueID == null) {
- SharedPreferences sharedPrefs = context.getSharedPreferences(
- MyBackupAgent.PREFS, Context.MODE_PRIVATE);
- uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
- if (uniqueID == null) {
- uniqueID = UUID.randomUUID().toString();
- Editor editor = sharedPrefs.edit();
- editor.putString(PREF_UNIQUE_ID, uniqueID);
- editor.commit();
- //backup the changes
- BackupManager mBackupManager = new BackupManager(context);
- mBackupManager.dataChanged();
- }
- }
- return uniqueID;
- }
- if API => 9:(95.4% of devices)
- return unique ID containing serial id(rooted phones may be different)
- else
- return unique ID of Build information(may overlap data - API < 9)
- /**
- * Return Pseudo Unique ID
- * @return ID
- */
- public static String getUniquePsuedoID()
- {
- // IF all else fails, if the user does is lower than API 9(lower
- // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
- // returns 'null', then simply the ID returned will be soley based
- // off their Android device information. This is where the collisions
- // can happen.
- // Thanks http://www.pocketmagic.net/?p=1662!
- // Try not to use DISPLAY, HOST or ID - these items could change
- // If there are collisions, there will be overlapping data
- String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
- // Thanks to @Roman SL!
- // http://stackoverflow.com/a/4789483/950427
- // Only devices with API >= 9 have android.os.Build.SERIAL
- // http://developer.android.com/reference/android/os/Build.html#SERIAL
- // If a user upgrades software or roots their phone, there will be a duplicate entry
- String serial = null;
- try
- {
- serial = android.os.Build.class.getField("SERIAL").toString();
- // go ahead and return the serial for api => 9
- return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
- }
- catch (Exception e)
- {
- // String needs to be initialized
- serial = "serial"; // some value
- }
- // Thanks @Joe!
- // http://stackoverflow.com/a/2853253/950427
- // Finally, combine the values we have found by using the UUID class to create a unique identifier
- return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
- }
- deviceId = Secure.getString(this.getContext().getContentResolver(), Secure.ANDROID_ID);
- final TelephonyManager tm = (TelephonyManager) getBaseContext()
- .getSystemService(SplashActivity.TELEPHONY_SERVICE);
- final String tmDevice, tmSerial, androidId;
- tmDevice = "" + tm.getDeviceId();
- Log.v("DeviceIMEI", "" + tmDevice);
- tmSerial = "" + tm.getSimSerialNumber();
- Log.v("GSM devices Serial Number[simcard] ", "" + tmSerial);
- androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(),
- android.provider.Settings.Secure.ANDROID_ID);
- Log.v("androidId CDMA devices", "" + androidId);
- UUID deviceUuid = new UUID(androidId.hashCode(),
- ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
- String deviceId = deviceUuid.toString();
- Log.v("deviceIdUUID universally unique identifier", "" + deviceId);
- String deviceModelName = android.os.Build.MODEL;
- Log.v("Model Name", "" + deviceModelName);
- String deviceUSER = android.os.Build.USER;
- Log.v("Name USER", "" + deviceUSER);
- String devicePRODUCT = android.os.Build.PRODUCT;
- Log.v("PRODUCT", "" + devicePRODUCT);
- String deviceHARDWARE = android.os.Build.HARDWARE;
- Log.v("HARDWARE", "" + deviceHARDWARE);
- String deviceBRAND = android.os.Build.BRAND;
- Log.v("BRAND", "" + deviceBRAND);
- String myVersion = android.os.Build.VERSION.RELEASE;
- Log.v("VERSION.RELEASE", "" + myVersion);
- int sdkVersion = android.os.Build.VERSION.SDK_INT;
- Log.v("VERSION.SDK_INT", "" + sdkVersion);
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- user@creep:~$ adb shell ls -l /sys/class/android_usb/android0/iSerial
- -rw-r--r-- root root 4096 2013-01-10 21:08 iSerial
- user@creep:~$ adb shell cat /sys/class/android_usb/android0/iSerial
- 0A3CXXXXXXXXXX5
- import android.Manifest.permission;
- import android.bluetooth.BluetoothAdapter;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.net.wifi.WifiManager;
- import android.provider.Settings.Secure;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- // TODO : hash
- public final class DeviceIdentifier {
- private DeviceIdentifier() {}
- /** @see http://code.google.com/p/android/issues/detail?id=10603 */
- private static final String ANDROID_ID_BUG_MSG = "The device suffers from "
- + "the Android ID bug - its ID is the emulator ID : "
- + IDs.BUGGY_ANDROID_ID;
- private static volatile String uuid; // volatile needed - see EJ item 71
- // need lazy initialization to get a context
- /**
- * Returns a unique identifier for this device. The first (in the order the
- * enums constants as defined in the IDs enum) non null identifier is
- * returned or a DeviceIDException is thrown. A DeviceIDException is also
- * thrown if ignoreBuggyAndroidID is false and the device has the Android ID
- * bug
- *
- * @param ctx
- * an Android constant (to retrieve system services)
- * @param ignoreBuggyAndroidID
- * if false, on a device with the android ID bug, the buggy
- * android ID is not returned instead a DeviceIDException is
- * thrown
- * @return a *device* ID - null is never returned, instead a
- * DeviceIDException is thrown
- * @throws DeviceIDException
- * if none of the enum methods manages to return a device ID
- */
- public static String getDeviceIdentifier(Context ctx,
- boolean ignoreBuggyAndroidID) throws DeviceIDException {
- String result = uuid;
- if (result == null) {
- synchronized (DeviceIdentifier.class) {
- result = uuid;
- if (result == null) {
- for (IDs id : IDs.values()) {
- try {
- result = uuid = id.getId(ctx);
- } catch (DeviceIDNotUniqueException e) {
- if (!ignoreBuggyAndroidID)
- throw new DeviceIDException(e);
- }
- if (result != null) return result;
- }
- throw new DeviceIDException();
- }
- }
- }
- return result;
- }
- private static enum IDs {
- TELEPHONY_ID {
- @Override
- String getId(Context ctx) {
- // TODO : add a SIM based mechanism ? tm.getSimSerialNumber();
- final TelephonyManager tm = (TelephonyManager) ctx
- .getSystemService(Context.TELEPHONY_SERVICE);
- if (tm == null) {
- w("Telephony Manager not available");
- return null;
- }
- assertPermission(ctx, permission.READ_PHONE_STATE);
- return tm.getDeviceId();
- }
- },
- ANDROID_ID {
- @Override
- String getId(Context ctx) throws DeviceIDException {
- // no permission needed !
- final String andoidId = Secure.getString(
- ctx.getContentResolver(),
- android.provider.Settings.Secure.ANDROID_ID);
- if (BUGGY_ANDROID_ID.equals(andoidId)) {
- e(ANDROID_ID_BUG_MSG);
- throw new DeviceIDNotUniqueException();
- }
- return andoidId;
- }
- },
- WIFI_MAC {
- @Override
- String getId(Context ctx) {
- WifiManager wm = (WifiManager) ctx
- .getSystemService(Context.WIFI_SERVICE);
- if (wm == null) {
- w("Wifi Manager not available");
- return null;
- }
- assertPermission(ctx, permission.ACCESS_WIFI_STATE); // I guess
- // getMacAddress() has no java doc !!!
- return wm.getConnectionInfo().getMacAddress();
- }
- },
- BLUETOOTH_MAC {
- @Override
- String getId(Context ctx) {
- BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
- if (ba == null) {
- w("Bluetooth Adapter not available");
- return null;
- }
- assertPermission(ctx, permission.BLUETOOTH);
- return ba.getAddress();
- }
- }
- // TODO PSEUDO_ID
- // http://www.pocketmagic.net/2011/02/android-unique-device-id/
- ;
- static final String BUGGY_ANDROID_ID = "9774d56d682e549c";
- private final static String TAG = IDs.class.getSimpleName();
- abstract String getId(Context ctx) throws DeviceIDException;
- private static void w(String msg) {
- Log.w(TAG, msg);
- }
- private static void e(String msg) {
- Log.e(TAG, msg);
- }
- }
- private static void assertPermission(Context ctx, String perm) {
- final int checkPermission = ctx.getPackageManager().checkPermission(
- perm, ctx.getPackageName());
- if (checkPermission != PackageManager.PERMISSION_GRANTED) {
- throw new SecurityException("Permission " + perm + " is required");
- }
- }
- // =========================================================================
- // Exceptions
- // =========================================================================
- public static class DeviceIDException extends Exception {
- private static final long serialVersionUID = -8083699995384519417L;
- private static final String NO_ANDROID_ID = "Could not retrieve a "
- + "device ID";
- public DeviceIDException(Throwable throwable) {
- super(NO_ANDROID_ID, throwable);
- }
- public DeviceIDException(String detailMessage) {
- super(detailMessage);
- }
- public DeviceIDException() {
- super(NO_ANDROID_ID);
- }
- }
- public static final class DeviceIDNotUniqueException extends
- DeviceIDException {
- private static final long serialVersionUID = -8940090896069484955L;
- public DeviceIDNotUniqueException() {
- super(ANDROID_ID_BUG_MSG);
- }
- }
- }
- String identifier = null;
- TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
- if (tm != null)
- identifier = tm.getDeviceId();
- if (identifier == null || identifier .length() == 0)
- identifier = Secure.getString(activity.getContentResolver(),Secure.ANDROID_ID);
- public static String getDeviceId(Context ctx)
- {
- TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
- String tmDevice = tm.getDeviceId();
- String androidId = Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID);
- String serial = null;
- if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) serial = Build.SERIAL;
- if(tmDevice != null) return "01" + tmDevice;
- if(androidId != null) return "02" + androidId;
- if(serial != null) return "03" + serial;
- // other alternatives (i.e. Wi-Fi MAC, Bluetooth MAC, etc.)
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement