Jack Chen
Jack Chen

Reputation: 39

How to get android device id?

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);
    }
}

Upvotes: 4

Views: 9179

Answers (1)

Akshay Katariya
Akshay Katariya

Reputation: 1474

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

Upvotes: 3

Related Questions