2

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.

mdb
  • 52,000
  • 11
  • 64
  • 62

3 Answers3

4

You can use the UUID class:

UUID.randomUUID().toString();
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Perception
  • 79,279
  • 19
  • 185
  • 195
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