Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8952e53

Browse files
committed
新增"如何取得android唯一码"、"为什么以下用随机生成的文字会得出 "hello world""
1 parent 8d14553 commit 8952e53

File tree

4 files changed

+185
-8
lines changed

4 files changed

+185
-8
lines changed

‎README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ stackoverflow-Java-top-qa
3636
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
3737
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
3838

39+
3940
> 编程技巧
4041
4142
* [去掉烦人的"!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
@@ -44,15 +45,16 @@ stackoverflow-Java-top-qa
4445
* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md)
4546
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
4647
* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
47-
48+
*[为什么以下用随机生成的文字会得出 "hello world"?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
4849

4950
> 网络
5051
5152
* [如何使用java.net.URLConnection接收及发送HTTP请求](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/using-java-net-urlconnection-to-fire-and-handle-http-requests.md)
5253

5354
> 性能
5455
55-
* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md)
56+
* [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/w
57+
* hen-to-use-linkedlist-over-arraylist.md)
5658
* [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md)
5759
* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md)
5860

@@ -61,24 +63,21 @@ stackoverflow-Java-top-qa
6163
* [如何测试 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)
6264

6365
> Android
64-
65-
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/troyliu0105/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
66+
*[在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
67+
* [如何取得android唯一码?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
6668

6769
### 待翻译问题链接(还剩x问题)
6870
- [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)
6971
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
7072
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
7173
- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
7274
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
73-
- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id)
74-
- [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)
7575
- [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)
7676
- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
7777
- [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)
7878
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
7979
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
8080
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
81-
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
8281
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
8382
- ['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)
8483
- [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)

‎contents/Declare-array-in-Java.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ String[] myStringArray = new String[]("a", "b", "c");
2323

2424

2525

26-
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
##如何取得android唯一码?
2+
3+
###问题
4+
每一个android装置都有唯一ID吗?如果有?怎么用java最简单取得呢?
5+
6+
###回答1(最佳)
7+
8+
如何取得android唯一码?
9+
好处:
10+
1.不需要特定权限.
11+
2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性.
12+
3.重装app之后仍能取得相同唯一值.
13+
14+
伪代码:
15+
16+
```
17+
if API => 9/10: (99.5% of devices)
18+
19+
return unique ID containing serial id (rooted devices may be different)
20+
21+
else
22+
23+
return unique ID of build information (may overlap data - API < 9)
24+
```
25+
26+
代码:
27+
28+
```java
29+
30+
/**
31+
* Return pseudo unique ID
32+
* @return ID
33+
*/public static String getUniquePsuedoID() {
34+
// If all else fails, if the user does have lower than API 9 (lower
35+
// than Gingerbread), has reset their device or 'Secure.ANDROID_ID'
36+
// returns 'null', then simply the ID returned will be solely based
37+
// off their Android device information. This is where the collisions
38+
// can happen.
39+
// Thanks http://www.pocketmagic.net/?p=1662!
40+
// Try not to use DISPLAY, HOST or ID - these items could change.
41+
// If there are collisions, there will be overlapping data
42+
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);
43+
44+
// Thanks to @Roman SL!
45+
// http://stackoverflow.com/a/4789483/950427
46+
// Only devices with API >= 9 have android.os.Build.SERIAL
47+
// http://developer.android.com/reference/android/os/Build.html#SERIAL
48+
// If a user upgrades software or roots their device, there will be a duplicate entry
49+
String serial = null;
50+
try {
51+
serial = android.os.Build.class.getField("SERIAL").get(null).toString();
52+
53+
// Go ahead and return the serial for api => 9
54+
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
55+
} catch (Exception exception) {
56+
// String needs to be initialized
57+
serial = "serial"; // some value
58+
}
59+
60+
// Thanks @Joe!
61+
// http://stackoverflow.com/a/2853253/950427
62+
// Finally, combine the values we have found by using the UUID class to create a unique identifier
63+
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();}
64+
```
65+
###回答2
66+
好处:
67+
1.不需要特定权限.
68+
2.在100% Android装置(包括root过的)上,保证唯一性.
69+
70+
坏处
71+
1.重装app之后不能取得相同唯一值.
72+
73+
```java
74+
private static String uniqueID = null;
75+
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
76+
77+
public synchronized static String id(Context context) {
78+
if (uniqueID == null) {
79+
SharedPreferences sharedPrefs = context.getSharedPreferences(
80+
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
81+
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
82+
if (uniqueID == null) {
83+
uniqueID = UUID.randomUUID().toString();
84+
Editor editor = sharedPrefs.edit();
85+
editor.putString(PREF_UNIQUE_ID, uniqueID);
86+
editor.commit();
87+
}
88+
}
89+
return uniqueID;
90+
}
91+
```
92+
93+
###回答3(需要有电话卡)
94+
95+
好处:
96+
1.重装app之后仍能取得相同唯一值.
97+
98+
代码:
99+
100+
```java
101+
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
102+
final String tmDevice, tmSerial, androidId;
103+
tmDevice = "" + tm.getDeviceId();
104+
tmSerial = "" + tm.getSimSerialNumber();
105+
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
106+
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
107+
String deviceId = deviceUuid.toString();
108+
```
109+
110+
谨记:要取得以下权限
111+
```
112+
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
113+
```
114+
stackoverflow链接:
115+
http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
##为什么以下用随机生成的文字会得出 "hello world"?
2+
3+
###问题
4+
为什么以下用随机生成的文字会得出"hello world".
5+
有人能解释一下吗?
6+
7+
```
8+
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
9+
10+
public static String randomString(int i)
11+
{
12+
Random ran = new Random(i);
13+
StringBuilder sb = new StringBuilder();
14+
while (true)
15+
{
16+
int k = ran.nextInt(27);
17+
if (k == 0)
18+
break;
19+
20+
sb.append((char)('`' + k));
21+
}
22+
return sb.toString();
23+
}
24+
```
25+
###回答1(最佳)
26+
在JAVA 里面,随机类的实现不是真正的随机,是伪随机.
27+
就是说如果随机类的种子是一样的话,他们会生成同一组的数字。
28+
29+
比如说这个问题:
30+
31+
new Random(-229985452).nextInt(27)
32+
33+
首6个生成的数字一定是:
34+
35+
8
36+
5
37+
12
38+
12
39+
15
40+
0
41+
42+
43+
`new Random(-147909649).nextInt(27)` 首6个生成的数字一定是:
44+
45+
23
46+
15
47+
18
48+
12
49+
4
50+
0
51+
52+
而把每一个数目字加 <code>`</code> (which is 96),就会得到了相应的英文字母:
53+
54+
8 + 96 = 104 --> h
55+
5 + 96 = 101 --> e
56+
12 + 96 = 108 --> l
57+
12 + 96 = 108 --> l
58+
15 + 96 = 111 --> o
59+
60+
23 + 96 = 119 --> w
61+
15 + 96 = 111 --> o
62+
18 + 96 = 114 --> r
63+
12 + 96 = 108 --> l
64+
4 + 96 = 100 --> d

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /