Rotem E
Rotem E

Reputation: 213

How to get android id in java without using of the class Activity?

I can't using the following method because my class doesn't extends the Activity class: Is there a unique Android device ID?

Upvotes: 5

Views: 1546

Answers (2)

Nithinlal
Nithinlal

Reputation: 5061

Try like this

public class Sample {


public static String getId(Context c)
{

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

     return android_id;
    }
}

when you need the device id

String id = Sample.getId(youractivity.this);// if ur in an activity

or try this

 String id =Sample.getId(getApplicationContext());

Upvotes: 0

George Thomas
George Thomas

Reputation: 4586

Try like this

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

public class sample {


    public String getId(Context c)
    {

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

         return android_id;
    }
}

pass the context from your activity

sample s=new sample();

    String udid=s.getId(getApplicationContext());
    Toast.makeText(getApplicationContext(), udid, Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions