How to get your Android device ID with Telephonymanager#getDeviceId()?

Asked

Viewed 2,415 times

3

I’m using this code to catch the ID of the device Android.

TelephonyManager telephonyManager = (TelephonyManager) getActivity().getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
id = telephonyManager.getDeviceId();

But now I’ve noticed that in some cases, the ID was recorded as null.

  • I’m not sure about getting the ID thus?
  • Under what circumstances does this return null?
  • Take a look at this link... http://stackoverflow.com/questions/7514163/using-telephony-manager-in-android-to-find-imei-number

  • So.. according to the link is right. It is a way to get the IMEI from the mobile. In the table where these Inserts were made I discovered some 15 cases where this value came null. Now I need to find out in which cases this happens.

  • It depends somewhat on its purpose of use. I suggest reading this article : https://developer.android.com/training/articles/user-data-ids.html But I warn you that there is a unique identifier generated at the time of installation that is registered on Google servers. This may suit you. See https://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID

2 answers

4


3

The getDeviceId() returns only value when the device is considering a phone.

If you want to get a "unique" identifier from the device you can use Secure.ANDROID_ID. Take an example:

import android.provider.Settings.Secure;
private String android_id = Secure.getString(
    getContext().getContentResolver(), Secure.ANDROID_ID);

However, you should be careful as it is possible to edit the device’s factory setting, for example by changing this value.

In best practices for unique identifiers says the following:

1: Avoid the use of hardware identifiers. Hardware identifiers such as SSAID (Android code) and IMEI can be avoided in most cases of use without limiting the necessary resources.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.