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

 android studio cant get device phone number

Go down 
AuthorMessage
Moti Barski

avatar

Posts : 246
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 :

Code:
package com.yotamarker.simstuff1;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    final int REQUEST_READ_PHONE_STATE = 0;
    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        ReadSIMPermission();
        // 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() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create().show();

        } else {
            ActivityCompat.requestPermissions(this,
                    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
-
» Trulia's Android Tablet Sweepstakes
» is it possible to find number of elements in an selectd xpath by selenium rc..?
» Phone scam
» wrong number of arguments provided in nunit
» how to store number of rows of a table

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