I wish to retrieve a unique number every time I start my program (from the internet) and to be able to have access to it in every class without passing it every time. Is this possible? I believe this must be somehow standard as uniqueID and is used everyhere but I am new to Android and Java.
Asked
Active
Viewed 182 times
2
-
2why do you want to retrieve it from the internet again? Is it not sufficient to generate a random number? – Dharini Chandrasekaran Mar 28 '12 at 00:04
-
No it is the result of authentication. – Mar 28 '12 at 00:21
3 Answers
4
You can use the UUID class:
UUID.randomUUID().toString();

Steve Kuo
- 61,876
- 75
- 195
- 257

Perception
- 79,279
- 19
- 185
- 195
-
to access it "from anywhere", you can assign it to a static member in an existing class or create a holder class for this purpose. – Jeffrey Blattman Mar 28 '12 at 00:09
2
Here is a previous solution that might be useful... Android global variable
Alternatively, you could declare a public static String, set the value of the String in the first class, and then each access to this String from other classes would return this same value.
For example...
Class 1...
public static String uniqueID;
uniqueID = <code_to_generate_random_value>;
Class 2, 3, 4..., where you want to get the value that was already set...
String uniqueID = Class1.uniqueID;

Community
- 1
- 1
1
you can use singleton pattern
create a class with desired name using singleton pattern
set the variable 'unique_id' to value which you get from internet or from uuid provider
use this classes singleton instance anywhere in your project and call unique_id's getter method

Ekrem
- 483
- 2
- 16