dave dave - 11 months ago 42
Android Question

get "DeviceId" and display it in textview

I'm new to android and I'm trying to get phone serial number and show it in text view..

so this is my code:

TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

String aa;

TextView tt = (TextView) findViewById(R.id.phoneidtxtview);

but I get this error:

The method setText(TelephonyManager) is undefined for the type String

i have checked this page and i know the return of the top code is "string"... so what is the problem?

Answer Source

aa is the object of class String. It has no method called setText(). This is what you've to do :

Declare this permission in the manifest file :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Now get DeviceId() in this way :

    String identifier = null;
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null)
        identifier = tm.getDeviceId();
    if (identifier == null || identifier .length() == 0)
        identifier = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

Set the identifier as text of TextView tt

TextView tt = (TextView) findViewById(R.id.phoneidtxtview);