gpt4 book ai didi

java - android - 访问所有 Activity 的 SharedPreferences

转载 作者:行者123 更新时间:2023-12-02 04:55:49 27 4
gpt4 key购买 nike

我正在制作一个应用程序,学生可以输入一次注册信息,然后就可以访问该应用程序的其余功能。

我的问题是,如何保存他要输入的注册号,将其保存在变量中并在应用程序中的其他 Activity 中使用它?

我有登录 Activity 的代码:

public class MainActivity2 extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);



et = (EditText)findViewById(R.id.rfid);
b = (Button)findViewById(R.id.loginnext);
tv = (TextView)findViewById(R.id.tv);

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("language", et.toString().toString());
editor.commit();

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity2.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}

void login(){
try{

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/check.php");

nameValuePairs = new ArrayList<NameValuePair>(2);

nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

response=httpclient.execute(httppost);

ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});

if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});

startActivity(new Intent(MainActivity2.this, MainActivity3Activity.class));
}else{
showAlert();
}

}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
MainActivity2.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}

}

我不确定我是否执行了 SharePreferences 操作,将其放置在正确的类中。

此外,我如何知道是否可以将 SharedPreferences 用于其他 Activity ?

感谢您的提前帮助。

最佳答案

像这样创建共享首选项;

SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
//Now put values in editor
editor.putString(key_value_for_access ,string_value_to_store );
editor.commit();

//现在要在每个 Activity 中访问它,您必须编写相同的内容:

SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
if(prefernces.contains(key_value_for_access)){
//getting data
String get = preferences.getString(key_value_for_access, null);
}

关于java - android - 访问所有 Activity 的 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784621/

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