4

How to get android device id? I don't know whats the "context".

import android.content.Context;
import android.provider.Settings;

public class getDeviceID {

    public void getAndroidID(Context context) {
        String android_id= Settings.System.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);

        System.out.println(android_id);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jack Chen
  • 39
  • 1
  • 5
  • Possible duplicate of [Is there a unique Android device ID?](https://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) – Kartik Shandilya Sep 02 '17 at 02:43
  • Context is the instance of the current class in which you will call this method. – Deepak Rana Sep 02 '17 at 02:52
  • 2
    @DeepakRana No it isn't. Its the Activity, Service, or Application which is running. Not the class. If that's all it was, you'd use this. – Gabe Sechan Sep 02 '17 at 03:12
  • if you are fresher then please study basics from tutorial web sites or android's developer site. and please search question before posting. – Rajesh N Sep 02 '17 at 04:07

1 Answers1

3

Create context for you application like this

private Context context = this;

Put this lines inside onCreate

    String android_id = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);

    Toast.makeText(context, "android_id= " + android_id, Toast.LENGTH_LONG).show();

And to know more about context refer this link

Akshay Katariya
  • 1,464
  • 9
  • 20