From f49ed6d8de4df2b05c6c93073cfcf3f8d0f9b2f8 Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Wed, 18 Nov 2015 17:20:48 +0800 Subject: [PATCH 01/16] is there a unique android device id added --- .../is-there-a-unique-android-device-id.md | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 contents/is-there-a-unique-android-device-id.md diff --git a/contents/is-there-a-unique-android-device-id.md b/contents/is-there-a-unique-android-device-id.md new file mode 100644 index 0000000..d92d84d --- /dev/null +++ b/contents/is-there-a-unique-android-device-id.md @@ -0,0 +1,115 @@ +##如何取得android唯一?? + +###?? +每一懸ndroidごm都有唯一ID??如果有?怎么用java最??取得呢? + +###回答1(最佳) + +如何取得android唯一?? +好]: +1.不需要特定?限. +2.在99.5% Androidごm(包括root?的)上,即API => 9,保?唯一性. +3.重饞pp之后仍能取得相同唯一值. + +?代?: + +``` +if API => 9/10: (99.5% of devices) + +return unique ID containing serial id (rooted devices may be different) + +else + +return unique ID of build information (may overlap data - API < 9) +``` + +代?: + +```java + +/** + * Return pseudo unique ID + * @return ID + */public static String getUniquePsuedoID() { + // If all else fails, if the user does have lower than API 9 (lower + // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' + // returns 'null', then simply the ID returned will be solely 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 device, there will be a duplicate entry + String serial = null; + try { + serial = android.os.Build.class.getField("SERIAL").get(null).toString(); + + // Go ahead and return the serial for api => 9 + return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); + } catch (Exception exception) { + // 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();} +``` +###回答2 +好]: +1.不需要特定?限. +2.在100% Androidごm(包括root?的)上,保?唯一性. + +坏] +1.重饞pp之后不能取得相同唯一值. + +```java +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; +} +``` + +###回答3(需要有m?卡) + +好]: +1.重饞pp之后仍能取得相同唯一值. + +代?: + +```java + 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(); +``` + +??:要取得以下?限 +``` + +``` +stackoverflow?接: +http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id \ No newline at end of file From 7bc60320fb0483d61f1fe656e828a1b4b913195d Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Wed, 18 Nov 2015 17:21:59 +0800 Subject: [PATCH 02/16] is there a unique android device id added --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a54c7e3..9234759 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ stackoverflow-Java-top-qa - [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) - [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) - [Why is printing B dramatically slower than printing #?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing) -- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) +- [憒雿敺android臭?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) - [Why does this code using random strings print hello world?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) - [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) - [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) From a12187cf28db7151cb21df15ca203c1febf7512a Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Wed, 18 Nov 2015 17:23:46 +0800 Subject: [PATCH 03/16] is there a unique android device id added --- .../is-there-a-unique-android-device-id.md | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/contents/is-there-a-unique-android-device-id.md b/contents/is-there-a-unique-android-device-id.md index d92d84d..7a0fbb8 100644 --- a/contents/is-there-a-unique-android-device-id.md +++ b/contents/is-there-a-unique-android-device-id.md @@ -1,17 +1,17 @@ -##如何取得android唯一?? +##憒雿敺android臭嚗 -###?? -每一懸ndroidごm都有唯一ID??如果有?怎么用java最??取得呢? +###桅 +瘥銝銝泳ndroid鋆蝵桅賣臭ID嚗憒嚗銋甫ava蝞敺g -###回答1(最佳) +###蝑1嚗雿喉 -如何取得android唯一?? -好]: -1.不需要特定?限. -2.在99.5% Androidごm(包括root?的)上,即API => 9,保?唯一性. -3.重饞pp之后仍能取得相同唯一值. +憒雿敺android臭嚗 +憟賢嚗 +1.銝閬孵. +2.99.5% Android鋆蝵殷毒oot餈嚗銝嚗莧PI => 9嚗靽霂臭. +3.鋆app銋隞賢敺詨臭. -?代?: +隡芯誨嚗 ``` if API => 9/10: (99.5% of devices) @@ -23,7 +23,7 @@ else return unique ID of build information (may overlap data - API < 9) ``` -代?: +隞: ```java @@ -62,13 +62,13 @@ return unique ID of build information (may overlap data - API < 9) // 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();} ``` -###回答2 -好]: -1.不需要特定?限. -2.在100% Androidごm(包括root?的)上,保?唯一性. +###蝑2 +憟賢嚗 +1.銝閬孵. +2.100% Android鋆蝵殷毒oot餈嚗銝嚗靽霂臭. -坏] -1.重饞pp之后不能取得相同唯一值. +憭 +1.鋆app銋銝賢敺詨臭. ```java private static String uniqueID = null; @@ -90,12 +90,12 @@ public synchronized static String id(Context context) { } ``` -###回答3(需要有m?卡) +###蝑3嚗閬菔∴ -好]: -1.重饞pp之后仍能取得相同唯一值. +憟賢嚗 +1.鋆app銋隞賢敺詨臭. -代?: +隞嚗 ```java final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); @@ -107,9 +107,9 @@ public synchronized static String id(Context context) { String deviceId = deviceUuid.toString(); ``` -??:要取得以下?限 +靚刻扇嚗閬敺隞乩 ``` ``` -stackoverflow?接: +stackoverflow暹伐 http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id \ No newline at end of file From 4627a0281ab6c77d04e316cffb9f61dfdb067551 Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Wed, 18 Nov 2015 17:23:51 +0800 Subject: [PATCH 04/16] is there a unique android device id added --- .idea/encodings.xml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .idea/encodings.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..74ace18 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 7cf1243011fc862c76871086d15cd162e8224719 Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 15:22:11 +0800 Subject: [PATCH 05/16] Go --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9234759..7f21240 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,15 @@ stackoverflow-Java-top-qa * [憒雿瘚霂 private 寞嚗函掩](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md) +> Android +- [憒雿敺android臭?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) + ### 敺蝧餉桅暹(餈官桅) - [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) - [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat) - [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) - [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) - [Why is printing B dramatically slower than printing #?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing) -- [憒雿敺android臭?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) - [Why does this code using random strings print hello world?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) - [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) - [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) From d3631f4e9b5a62e42815875a84a5630e8d3692bf Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 19:15:47 +0800 Subject: [PATCH 06/16] Go --- README.md | 5 ++--- contents/declare-array-in-java.md | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 contents/declare-array-in-java.md diff --git a/README.md b/README.md index 7f21240..1a4d40c 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ stackoverflow-Java-top-qa * [wait()sleep()箏侷(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md) * [賢血其銝芣( `constructor` )銝剛典虫銝芣沘(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md) * [ `finally` 隞颱鋡急扯銋](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md) - +* [甫ava銝剖ㄟ啁](http://stackoverflow.com/questions/1200621/declare-array-in-java) > 蝻蝔撌 * [餅虫犖!=null"(斤征霂剖包(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md) @@ -53,7 +53,7 @@ stackoverflow-Java-top-qa * [憒雿瘚霂 private 寞嚗函掩](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md) > Android -- [憒雿敺android臭?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) +* [憒雿敺android臭?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) ### 敺蝧餉桅暹(餈官桅) - [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) @@ -72,7 +72,6 @@ stackoverflow-Java-top-qa - [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) - [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) - ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips) -- [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java) - [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer) - [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error) - [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) diff --git a/contents/declare-array-in-java.md b/contents/declare-array-in-java.md new file mode 100644 index 0000000..18c003f --- /dev/null +++ b/contents/declare-array-in-java.md @@ -0,0 +1,32 @@ +##在java中\明?? + +###??描述: +你是如何在java中\明??的。 + +你可以?行用??\明或者??常量(注意:?你\明或者立即影鄐FZ量,??常量?不能再用?分配一??) +?于原始W型: +``` +int[] myIntArray = new int[3]; +int[] myIntArray = {1, 2, 3}; +int[] myIntArray = new int[]{1, 2, 3}; +``` +物件W,比如StringW,也是相同的: +``` +String[] myStringArray = new String[3]; +String[] myStringArray = {"a", "b","c"}; +String[] myStringArray = new String[]("a", "b", "c"); +``` + +注意: +1.??常量 +``` +//for a function return type is int[],you cannot return +{1,2,3} +//but you can return +new int[]{1, 2, 3}; +``` + +[stack overflow?接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) + + + From 9dca18a42c99813611c53e8e2310a4d6beb5682a Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 19:22:09 +0800 Subject: [PATCH 07/16] Go --- .idea/encodings.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 74ace18..6775912 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,7 @@ - + + \ No newline at end of file From 05499d48d72d780a836b7a834d036a11f6333290 Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 19:23:14 +0800 Subject: [PATCH 08/16] Go --- .idea/encodings.xml | 1 + contents/declare-array-in-java.md | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 6775912..1004fb2 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,7 @@ + diff --git a/contents/declare-array-in-java.md b/contents/declare-array-in-java.md index 18c003f..fbd65e9 100644 --- a/contents/declare-array-in-java.md +++ b/contents/declare-array-in-java.md @@ -1,24 +1,24 @@ -##在java中\明?? +##甫ava銝剖ㄟ啁 -###??描述: -你是如何在java中\明??的。 +###桅餈堆 +雿臬雿甫ava銝剖ㄟ啁 -你可以?行用??\明或者??常量(注意:?你\明或者立即影鄐FZ量,??常量?不能再用?分配一??) -?于原始W型: +雿臭誑餈銵冽啁憯唳啁撣賊嚗瘜冽嚗敶雿憯唳蝡喳蔣鈭嚗啁撣賊撠銝賢冽亙銝銝芣啁嚗 +撖嫣憪蝐餃嚗 ``` int[] myIntArray = new int[3]; int[] myIntArray = {1, 2, 3}; int[] myIntArray = new int[]{1, 2, 3}; ``` -物件W,比如StringW,也是相同的: +拐辣蝐鳴瘥憒String蝐鳴銋舐詨嚗 ``` String[] myStringArray = new String[3]; String[] myStringArray = {"a", "b","c"}; String[] myStringArray = new String[]("a", "b", "c"); ``` -注意: -1.??常量 +瘜冽: +1.啁撣賊 ``` //for a function return type is int[],you cannot return {1,2,3} @@ -26,7 +26,7 @@ String[] myStringArray = new String[]("a", "b", "c"); new int[]{1, 2, 3}; ``` -[stack overflow?接:Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) +[stack overflow?伐Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) From 218f39bb73a5cb2c6aa02e455d4176787fb77d0b Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 19:23:47 +0800 Subject: [PATCH 09/16] Go --- contents/declare-array-in-java.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/declare-array-in-java.md b/contents/declare-array-in-java.md index fbd65e9..7783401 100644 --- a/contents/declare-array-in-java.md +++ b/contents/declare-array-in-java.md @@ -26,7 +26,7 @@ String[] myStringArray = new String[]("a", "b", "c"); new int[]{1, 2, 3}; ``` -[stack overflow?伐Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) +[stack overflow暹伐Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) From b3d797ee3714e09b202355647e604eca9da26bc4 Mon Sep 17 00:00:00 2001 From: Jeff Chung Date: Mon, 23 Nov 2015 19:24:27 +0800 Subject: [PATCH 10/16] Go --- README.md | 2 +- contents/declare-array-in-java.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1a4d40c..ee0e5db 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ stackoverflow-Java-top-qa * [wait()sleep()箏侷(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md) * [賢血其銝芣( `constructor` )銝剛典虫銝芣沘(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md) * [ `finally` 隞颱鋡急扯銋](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md) -* [甫ava銝剖ㄟ啁](http://stackoverflow.com/questions/1200621/declare-array-in-java) +* [甫ava銝剖ㄟ啁](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) > 蝻蝔撌 * [餅虫犖!=null"(斤征霂剖包(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md) diff --git a/contents/declare-array-in-java.md b/contents/declare-array-in-java.md index 7783401..8ae8846 100644 --- a/contents/declare-array-in-java.md +++ b/contents/declare-array-in-java.md @@ -26,7 +26,7 @@ String[] myStringArray = new String[]("a", "b", "c"); new int[]{1, 2, 3}; ``` -[stack overflow暹伐Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) +[Stackoverflow暹伐Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java#) From 664903294ccf013420b3a3f0719b7b9b576d48e8 Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 15:47:50 +0800 Subject: [PATCH 11/16] update --- .idea/encodings.xml | 8 -------- README.md | 1 - 2 files changed, 9 deletions(-) delete mode 100644 .idea/encodings.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 1004fb2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index ee0e5db..8cbd78d 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,6 @@ stackoverflow-Java-top-qa - [implements Runnable vs. extends Thread](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) - [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java) - [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk) -- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) - [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) - ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips) - [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer) From 0ec6924f17c6fe1a5e877333b09b4c4246a445dd Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 16:30:55 +0800 Subject: [PATCH 12/16] update --- README.md | 5 +- ...-using-random-strings-print-hello-world.md | 139 ++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 contents/why-does-this-code-using-random-strings-print-hello-world.md diff --git a/README.md b/README.md index 8cbd78d..b0195dc 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ stackoverflow-Java-top-qa * [賢血其銝芣( `constructor` )銝剛典虫銝芣沘(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md) * [ `finally` 隞颱鋡急扯銋](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md) * [甫ava銝剖ㄟ啁](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) + +> +[銝箔銋隞乩券箇摮隡敺 hello world?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) + > 蝻蝔撌 * [餅虫犖!=null"(斤征霂剖包(https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md) @@ -61,7 +65,6 @@ stackoverflow-Java-top-qa - [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) - [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) - [Why is printing B dramatically slower than printing #?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing) -- [Why does this code using random strings print hello world?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) - [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) - [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) - [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed) diff --git a/contents/why-does-this-code-using-random-strings-print-hello-world.md b/contents/why-does-this-code-using-random-strings-print-hello-world.md new file mode 100644 index 0000000..0a48c88 --- /dev/null +++ b/contents/why-does-this-code-using-random-strings-print-hello-world.md @@ -0,0 +1,139 @@ +##銝箔銋隞乩券箇摮隡敺 hello world? + +###桅 +The following print statement would print "hello world". +Could anyone explain this? + +``` +System.out.println(randomString(-229985452) + " " + randomString(-147909649)); +``` + +And `randomString()` looks like this: + +``` +public static String randomString(int i) +{ + Random ran = new Random(i); + StringBuilder sb = new StringBuilder(); + while (true) + { + int k = ran.nextInt(27); + if (k == 0) + break; + + sb.append((char)('`' + k)); + } + return sb.toString(); +} +``` + +###蝑1嚗雿喉 + +憒雿敺android臭嚗 +憟賢嚗 +1.銝閬孵. +2.99.5% Android鋆蝵殷毒oot餈嚗銝嚗莧PI => 9嚗靽霂臭. +3.鋆app銋隞賢敺詨臭. + +隡芯誨嚗 + +``` +if API => 9/10: (99.5% of devices) + +return unique ID containing serial id (rooted devices may be different) + +else + +return unique ID of build information (may overlap data - API < 9) +``` + +隞: + +```java + +/** + * Return pseudo unique ID + * @return ID + */public static String getUniquePsuedoID() { + // If all else fails, if the user does have lower than API 9 (lower + // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' + // returns 'null', then simply the ID returned will be solely 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 device, there will be a duplicate entry + String serial = null; + try { + serial = android.os.Build.class.getField("SERIAL").get(null).toString(); + + // Go ahead and return the serial for api => 9 + return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); + } catch (Exception exception) { + // 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();} +``` +###蝑2 +憟賢嚗 +1.銝閬孵. +2.100% Android鋆蝵殷毒oot餈嚗銝嚗靽霂臭. + +憭 +1.鋆app銋銝賢敺詨臭. + +```java +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; +} +``` + +###蝑3嚗閬菔∴ + +憟賢嚗 +1.鋆app銋隞賢敺詨臭. + +隞嚗 + +```java + 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(); +``` + +靚刻扇嚗閬敺隞乩 +``` + +``` +stackoverflow暹伐 +http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id \ No newline at end of file From 49e4ee2492268135ef652082c4c874d8f3f7a0d0 Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 16:45:44 +0800 Subject: [PATCH 13/16] update --- ...-using-random-strings-print-hello-world.md | 139 ++++-------------- 1 file changed, 32 insertions(+), 107 deletions(-) diff --git a/contents/why-does-this-code-using-random-strings-print-hello-world.md b/contents/why-does-this-code-using-random-strings-print-hello-world.md index 0a48c88..c546367 100644 --- a/contents/why-does-this-code-using-random-strings-print-hello-world.md +++ b/contents/why-does-this-code-using-random-strings-print-hello-world.md @@ -1,16 +1,12 @@ ##銝箔銋隞乩券箇摮隡敺 hello world? ###桅 -The following print statement would print "hello world". -Could anyone explain this? +銝箔銋隞乩券箇摮隡敺"hello world". +鈭箄質圾銝銝? ``` System.out.println(randomString(-229985452) + " " + randomString(-147909649)); -``` - -And `randomString()` looks like this: -``` public static String randomString(int i) { Random ran = new Random(i); @@ -26,114 +22,43 @@ public static String randomString(int i) return sb.toString(); } ``` - ###蝑1嚗雿喉 +沅AVA g箇掩摰唬舐甇,臭憚. +撠望航秩憒箇掩蝘摮臭瑞霂嚗隞隞砌銝蝏啣 -憒雿敺android臭嚗 -憟賢嚗 -1.銝閬孵. -2.99.5% Android鋆蝵殷毒oot餈嚗銝嚗莧PI => 9嚗靽霂臭. -3.鋆app銋隞賢敺詨臭. - -隡芯誨嚗 - -``` -if API => 9/10: (99.5% of devices) - -return unique ID containing serial id (rooted devices may be different) - -else +瘥憒霂渲銝芷桅: -return unique ID of build information (may overlap data - API < 9) -``` - -隞: + new Random(-229985452).nextInt(27) -```java +擐6銝芰啣銝摰: -/** - * Return pseudo unique ID - * @return ID - */public static String getUniquePsuedoID() { - // If all else fails, if the user does have lower than API 9 (lower - // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' - // returns 'null', then simply the ID returned will be solely 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); + 8 + 5 + 12 + 12 + 15 + 0 - // 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 device, there will be a duplicate entry - String serial = null; - try { - serial = android.os.Build.class.getField("SERIAL").get(null).toString(); - // Go ahead and return the serial for api => 9 - return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); - } catch (Exception exception) { - // String needs to be initialized - serial = "serial"; // some value - } + `new Random(-147909649).nextInt(27)` 擐6銝芰啣銝摰: - // 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();} -``` -###蝑2 -憟賢嚗 -1.銝閬孵. -2.100% Android鋆蝵殷毒oot餈嚗銝嚗靽霂臭. + 23 + 15 + 18 + 12 + 4 + 0 -憭 -1.鋆app銋銝賢敺詨臭. +瘥銝銝芣啁桀 ` (which is 96)嚗撠曹敺唬詨望摮瘥: -```java -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; -} -``` - -###蝑3嚗閬菔∴ - -憟賢嚗 -1.鋆app銋隞賢敺詨臭. - -隞嚗 - -```java - 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(); -``` - -靚刻扇嚗閬敺隞乩 -``` - -``` -stackoverflow暹伐 -http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id \ No newline at end of file + 8 + 96 = 104 --> h + 5 + 96 = 101 --> e + 12 + 96 = 108 --> l + 12 + 96 = 108 --> l + 15 + 96 = 111 --> o + + 23 + 96 = 119 --> w + 15 + 96 = 111 --> o + 18 + 96 = 114 --> r + 12 + 96 = 108 --> l + 4 + 96 = 100 --> d \ No newline at end of file From 940690de440f51864fbd89b004d004d9b3e747e3 Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 16:46:17 +0800 Subject: [PATCH 14/16] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b0195dc..bc8fcfa 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ stackoverflow-Java-top-qa * [甫ava銝剖ㄟ啁](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) > -[銝箔銋隞乩券箇摮隡敺 hello world?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world) +[銝箔銋隞乩券箇摮隡敺 hello world?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md) > 蝻蝔撌 From 93b4d38ccfc88dc9426f35bebf951fce539e9a6f Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 16:46:41 +0800 Subject: [PATCH 15/16] update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bc8fcfa..858167d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ stackoverflow-Java-top-qa > Android * [憒雿敺android臭?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) +嚗,嚗,嚗 ### 敺蝧餉桅暹(餈官桅) - [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) - [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat) From c2e6492e388e8eeb4dc5499a7356d9076ea703ec Mon Sep 17 00:00:00 2001 From: "jeff.chung.123123" Date: Sat, 28 Nov 2015 16:47:54 +0800 Subject: [PATCH 16/16] update --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 858167d..8ee0926 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ stackoverflow-Java-top-qa * [甫ava銝剖ㄟ啁](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) > -[銝箔銋隞乩券箇摮隡敺 hello world?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md) + +* [銝箔銋隞乩券箇摮隡敺 hello world?](https://github.com/chungchi300/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md) > 蝻蝔撌