我有两个活动,第一个活动“LoginActivity”,第二个活动“student_activity”。请告诉我如何从第二个活动调用方法“调用”并将值 bool 返回到第一个活动,以便知道用户是否 id 和密码正确或不正确。第一个活动从“edittext”获取 ID 和密码,然后将 ID 和密码发送到第二个活动中的方法,以确保从服务器的数据。
代码第一个活动是:
public class LoginActivity extends Activity{
EditText EdtId;
EditText EdtPassword;
Button btn1;
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
prefs = this.getSharedPreferences(this.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
EdtId=(EditText)findViewById(R.id.IdStudent);
EdtPassword=(EditText)findViewById(R.id.PasswordStudent);
btn1=(Button)findViewById(R.id.btnLogin);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
createLoginSession(Integer.parseInt(EdtId.getText().toString()),EdtPassword.getText().toString());
//here call second activity for sure from data
Intent intent = new Intent(LoginActivity.this,tofi.android.Student_Activity.class);
startActivity(intent);
finish();
startActivity(new Intent(LoginActivity.this,com.jcxavier.widget.test.BadgeButtonTestActivity.class));
}
});
}
//this method store data in SharedPreferences for get this data in second activity
public void createLoginSession(int id, String password){
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("id", id);
editor.putString("password", password);
editor.commit();
}
}
代码第二个活动是:
public class Student_Activity {
SharedPreferences prefs = this.getSharedPreferences(this.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.program_student);
NewsLoader call = new NewsLoader();
call.execute(this, true);
}
private Context context;
private boolean pullFromServer = false;
class NewsLoader extends AsyncTask<Object, Void, List<student>> {
private Context context;
private boolean pullFromServer = false;
protected List<student> doInBackground(Object... params) {
context = (Context) params[0];
pullFromServer = (Boolean) params[1];
dataSource.open();
if (pullFromServer) {
//get attribute from SharedPreferences
int id = prefs.getInt("id", 24);
String password = prefs.getString("password","wce");
// studentHandler class for sure password content method call for send to server and //return value if correct or not correct and return value type bool.
bool s;
s = StudentHandler.getInstance().call(context,id,password);
}
}
}