4

How can I get the android device id which I get on dialing *#*#8255#*#*

I am using the following code but it does not return the same...what I want

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

enter image description here

Neeraj
  • 499
  • 8
  • 16

1 Answers1

5

I think the code you tried probably returns a dynamic ID. Try this.

private static final Uri URI = Uri.parse("content://com.google.android.gsf.gservices"); private static final String ID_KEY = "android_id";

   String getAndroidId(Context ctx) {
       String[] params = { ID_KEY };
       Cursor c = ctx.getContentResolver()
               .query(URI, null, null, params, null);
       try{

       if (!c.moveToFirst() || c.getColumnCount() < 2){
           return null;
       }
       }catch(Exception e){
           return null;
       }

       try {
           Toast.makeText(ctx, Long.toHexString(Long.parseLong(c.getString(1))), 500).show();
           System.out.println("android id  " + Long.toHexString(Long.parseLong(c.getString(1))));

           return Long.toHexString(Long.parseLong(c.getString(1)));

       } catch (NumberFormatException e) {
           return null;
       }
   }

In the above code Long.toHexString(Long.parseLong(c.getString(1))), returns the android device id.

Oam
  • 912
  • 6
  • 7