-3

I need to know peoples phone number for the app I'm making, because the app revolves around the phone number. Since I can't find any foolproof way of getting the A Number off a phone, I want the very first time someone opens the app, them to enter their phone number. And I can't seem to find something similar to this on google.

Any lads here that got any ideas on how to make this work?

PS. I'm rather new to Android programming and programming in general!

Mikkel Larsen
  • 876
  • 2
  • 14
  • 26

2 Answers2

0

You can get the phone number with

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);  
String mPhoneNumber = tMgr.getLine1Number();

And in Manifest:

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

However this wont work all off the time (some comments in similar questions here on SO like this even suggest it hardly works at all).

So what you should do is prompt the user when the app starts, get his input from a textfield or something and store it somewhere you will be able to get it. The best way for you might be to store it in SharedPreferences:

SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);


String phoneNumber = ""; 

//save
prefs.edit().putString("phoneNumber", phoneNumber).apply();

//read
phoneNumber = prefs.getString(phoneNumber, ""); 

Similar to this you can save a bool (putBoolean and getBoolean) after you first started the app, so you can keep track of that. Just set it to false after you got the number and check for it every time you start the app again.

Community
  • 1
  • 1
tritop
  • 1,655
  • 2
  • 18
  • 30
0

Add a new activity and from AndroidManifest.xml make it the launcher activity

public class PhoneActivity extends Activity {
String number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkStatus();
    //considering that SliderActivity is your current main activity

        if (number.equals("")) { //finish this activity if number already present
        Intent sliderIntent = new Intent(PhoneActivity .this,
                SliderActivity.class);
        startActivity(sliderIntent);
        finish();
    } 


  //In xml create an edittext

   phoneEditText.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
         SharedPreferences.Editor editor = settings.edit();
         editor.putString("number", phoneEditText.getText().toString());
         Intent sliderIntent = new Intent(PhoneActivity .this,
                SliderActivity.class);
        startActivity(sliderIntent);
        finish();
        }
    });

}

    public void checkStatus()
    {
    SharedPreferences settings = getSharedPreferences("myPref", 0);
    number= settings.getString("number", "");
    }

}
Ravi Sharma
  • 753
  • 1
  • 7
  • 13
  • I suggest you edit your code because there is a normal line in & there are some "}" that are not correct. – Abkarino Oct 20 '14 at 12:51
  • Well to get it to work we had to change it a lot, since your if isn't making sense, but we managed to work something out by your example, thanks! – Mikkel Larsen Oct 23 '14 at 11:27