gpt4 book ai didi

android - 使用内部存储来存储凭据 Android

转载 作者:行者123 更新时间:2023-11-29 16:16:09 25 4
gpt4 key购买 nike

我正在尝试将三个字符串永久存储为我的 Android 应用程序的用户首选项。这三个字符串是 url、用户名和密码。我不太了解SharedPreferences,所以我尝试使用内部文件存储。我无法从文件中检索三个字符串,并且出现运行时错误。我知道我可能编码有误,但我对 Android 的精通程度不足以理解数据存储。有人可以帮帮我吗?

偏好 Activity :

package com.amritayalur.mypowerschool;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;
FileOutputStream fos;
String url = "";
String FILENAME = "InternalStrings";
String str;
String username;
String password;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
textViewTitle = (TextView) findViewById(R.id.textViewTitle);
textViewDesc = (TextView) findViewById(R.id.textViewDesc);

editTextURL = (EditText) findViewById(R.id.editTextURL);
editTextUser = (EditText) findViewById(R.id.editTextUser);
editTextPass = (EditText) findViewById(R.id.editTextPass);
//Start TextView
textViewTitle.setText("MyPowerSchool");
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//button listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
if ( ( !editTextURL.getText().toString().equals("")) && (
!editTextUser.getText().toString().equals("")) && (
!editTextPass.getText().toString().equals("") ) )
{
url = editTextURL.getText().toString();
username = editTextUser.getText().toString();
password = editTextPass.getText().toString();
//Saving data via File
/* File f = new File(FILENAME);
try {
fos = new FileOutputStream(f);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

fos.write(url.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(username.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(password.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



// TODO Auto-generated method stub
Intent i = new Intent( MyPowerSchoolActivity.this,
creds.class);
//i.putExtra("pschoolurl", editTextURL.getText().toString());
//i.putExtra("pschooluser", editTextUser.getText().toString());
//i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
}
};




});}}

我正在尝试检索凭据的 Activity :

package com.amritayalur.mypowerschool;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;



public class creds extends Activity {

String url;
String username;
String password;
TextView TextViewTest;

String FILENAME = "InternalStrings";
;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);




Intent intent = getIntent();
//String url = intent.getExtras().getString("pschoolurl");
//String username = intent.getExtras().getString("pschooluser");
//String password = intent.getExtras().getString("pschoolpass");
String collected = null;
FileInputStream fis = null;
try {
fis = openFileInput(FILENAME);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1 ){
collected = new String(dataArray);

}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
TextViewTest.setText(collected);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}






}
}

注释文本是我试图弄乱代码的不同方面的结果。

最佳答案

SharedPreferences 并不太难。

向首选项添加内容:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this); //Get the preferences
Editor edit = prefs.edit(); //Needed to edit the preferences

edit.putString("name", "myname"); //add a String
edit.putBoolean("rememberCredentials", true); //add a boolean

edit.commit(); // save the edits.

阅读一些东西:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this); //Get the preferences
String name = prefs.getString("name", "defaultName"); //get a String
boolean rememberCredentials = prefs.getBoolean("rememberCredentials", true); //get a boolean.
//When the key "rememberCredentials" is not present, true is returned.

关于android - 使用内部存储来存储凭据 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com