Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
Rotem E Avatar asked Nov 11 '22 06:11

Rotem E


1 Answers

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();
like image 78
George Thomas Avatar answered Nov 14 '22 22:11

George Thomas