我开发了一个android应用程序,使用户可以通过他/她的手机号码进行注册。我希望我的应用程序保存电话号码,以便下次用户打开应用程序时,不需要再次输入电话号码,类似于Whatsapp .. 这是我的代码,但它不起作用,我每次打开应用程序时都必须输入电话号码,此外,在将此代码添加到我的应用程序后,应用程序变得如此沉重而缓慢。手机号码保存在一个文件中android
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);
}
}
请帮我弄明白了!