Question

I have written a simple android application to fetch the phone number... this application is working in Android emulator.... but it is not working in Android mobile phones(both in samsung galaxy s3 and samsung ace).

Here is the source code.

PhoneNumberActivity.java

public class PhoneNumberActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_number);


        final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.getLine1Number();

        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {

                TextView  textView = (TextView) findViewById(R.id.phoneNumber);
                textView.setText("Phone Number is :- "+telephonyManager.getLine1Number());
                button.setVisibility(Button.INVISIBLE);
                textView.setVisibility(TextView.VISIBLE);
            }
        });
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pervazive.phonenumber"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.phonenumber.PhoneNumberActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 </manifest>

Please let me know what is the problem.

Was it helpful?

Solution

According to the documentation .getLine1Number() "Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable. "

Apparently .getLine1Number() reads this information from SIM card, so if the operator has set the MSISDN field it will return you its value and null if they did not set this field.

In your case probably your SIM card does not have this field populated by operator.

For More Info You can Visit this.

I got this from here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top