battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 

 android studio cant get device phone number

Go down 
Moti Barski

Moti Barski

Posts : 271
Join date : 2011-08-02

PostSubject: android studio cant get device phone number   Fri Sep 28, 2018 6:30 pm

AndroidManifest.xml :

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

ActivityMain :

package com.yotamarker.simstuff1;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    final int REQUEST_READ_PHONE_STATE = 0;
    protected void onCreate(Bundle savedInstanceState) {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // get SIM card ID
//        String simID = null;
//        if(ReadSIMPermission()){simID = telephonyManager.getSimSerialNumber();}
//        if(simID!=null){ Toast.makeText(this,"sim ID number : " +simID,Toast.LENGTH_LONG).show();}
        // get telNumber
        String telNumber = null;
        if(ReadSIMPermission()){telNumber = telephonyManager.getLine1Number();}
        if(telNumber!=null){ Toast.makeText(this,"tel number : " + telNumber,Toast.LENGTH_LONG).show();}

    public Boolean ReadSIMPermission(){
        Boolean granted =false ;
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {

            new AlertDialog.Builder(this)
                    .setTitle("Permission needed")
                    .setMessage("This permission is needed to get your SIM card data")
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                                    new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

        } else {
                    new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
            granted = true;

        return granted;


for some reason telephonyManager.getLine1Number() returns null instead of the actuall device's
phone number

MB over and out _______________________     
Back to top Go down
View user profile
android studio cant get device phone number
Back to top 
Page 1 of 1
 Similar topics
» Retrieving number of rows in a table
» how to count number of items present in dropdown
» program to provide total number of objects present / available on the page
» Need to randomly generate a DOB in SeleniumIDE
» Connected to TENSE...

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: