Android 将手机号码保存在文件中
我开发了一个android应用程序,用户可以通过手机号码注册。我希望我的应用程序保存电话号码,以便下次用户打开应用程序时,无需再次输入电话号码,类似于Whatsapp。。 这是我的代码,但它不起作用,每次打开应用程序时我都必须输入电话号码,而且,在将此代码添加到我的应用程序后,应用程序变得又重又慢Android 将手机号码保存在文件中,android,android-intent,try-catch,fileinputstream,fileoutputstream,Android,Android Intent,Try Catch,Fileinputstream,Fileoutputstream,我开发了一个android应用程序,用户可以通过手机号码注册。我希望我的应用程序保存电话号码,以便下次用户打开应用程序时,无需再次输入电话号码,类似于Whatsapp。。 这是我的代码,但它不起作用,每次打开应用程序时我都必须输入电话号码,而且,在将此代码添加到我的应用程序后,应用程序变得又重又慢 if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new Str
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);
}
}
请帮我弄清楚 在这段代码中:
if (mPhoneNumber == null) {
try {
fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE);
fIn = openFileInput("textfile.txt");
您打开文件进行输出,这将销毁您已经写入的任何内容。稍后,当您尝试读取此文件时,一切都太晚了
而且,这里的代码太多了。不要重新发明轮子。您不需要一次读取一个字符的文件。如果您只想将字符串写入文件,然后再将其读回,请使用DataInputStream
和DataOutputStream
,您可以直接使用readUTF()
和writeUTF()
读取/写入字符串。下面是一个读取该文件的简单示例:
DataInputStream in = new DataInputStream(openFileInput("textfile.txt"));
String contents = in.readUTF();
要写入文件,请使用:
DataOuputStream out = new DataOutputStream(openFileOutput("textfile.txt", 0));
out.writeUTF(phoneNumber);
显然,您需要添加try/catch块和处理异常,并确保在
finally
块中关闭流,但如果这样做,您将得到更少的代码。为了帮助您,我给出了我的活动示例,以便读取和写入文件中的数据:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class StoreDataActivity extends Activity {
private static final String TAG = "ExerciceActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
writeFileOnDisk("toto.txt", "Bienvenue chez Android");
String content = readFileOnDisk("toto.txt");
Log.v(TAG, "content=" + content);
}
private void writeFileOnDisk(String filename, String data) {
try {
FileOutputStream fos = openFileOutput(filename, this.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String readFileOnDisk(String filename) {
int inChar;
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = this.openFileInput(filename);
while ((inChar = fis.read()) != -1) {
buffer.append((char) inChar);
}
fis.close();
String content = buffer.toString();
return content;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}