battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeSearchRegisterLog in
Share
 

 android studio cant get device phone number

Go down 
AuthorMessage
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

android studio cant get device phone number Empty
PostSubject: android studio cant get device phone number   android studio cant get device phone number EmptyFri 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 emp it up
Back to top Go down
 
android studio cant get device phone number
Back to top 
Page 1 of 1

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