I have developed an android app that enable the user to register by his/her mobile phone number. I want my app to save the phone number so that next time the user open the app, there will be no need to enter the phone number again, analogous to Whatsapp.. Here is my code, but it doesn't work and I still have to enter the phone number each time I open the app, besides, after adding this code to my app, the app became so heavy and slow.
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {
TelephonyManager tMgr = (TelephonyManager) getApplicationContext()
.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number().toString();
} catch (Exception e) {
String EE = e.getMessage();
}
if (mPhoneNumber == null) {
try {
fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE);
fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[50];
if (isr.read(inputBuffer) == 0) {
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warrning");
alert.setMessage("Please Set Your Phone number");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mPhoneNumber = input.getText().toString();
try {
fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[50];
if (isr.read(inputBuffer) == 0) {
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// ---write the string to the file---
osw.write(mPhoneNumber);
osw.flush();
osw.close();
// ---display file saved message---
Toast.makeText(getBaseContext(),
"Phone number saved successfully!",
Toast.LENGTH_SHORT).show();
// ---clears the EditText---
input.setText("");
} else {
int charRead;
while ((charRead = isr.read(inputBuffer)) > 0) {
// ---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer,
0, charRead);
mPhoneNumber = readString;
inputBuffer = new char[50];
}
// ---set the EditText to the text that has been
// read---
Toast.makeText(getBaseContext(),
"Phone number read successfully!",
Toast.LENGTH_SHORT).show();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
int UserServiceId = CallLogin(mPhoneNumber);
if (UserServiceId > 0) {
Intent Service = new Intent(MainScreeen.this,
RecipeService.class);
Service.putExtra("UserId", UserServiceId);
startService(Service);
} else {
Intent Reg = new Intent(MainScreeen.this,
Regsteration.class);
Reg.putExtra("PhoneNumber", mPhoneNumber);
startActivity(Reg);
}
}
});
alert.show();
} else {
int UserServiceId = CallLogin(mPhoneNumber);
if (UserServiceId > 0) {
Intent Service = new Intent(MainScreeen.this,
RecipeService.class);
Service.putExtra("UserId", UserServiceId);
startService(Service);
} else {
Intent Reg = new Intent(MainScreeen.this, Regsteration.class);
Reg.putExtra("PhoneNumber", mPhoneNumber);
startActivity(Reg);
}
}
Please help me to figure it out !!