尝试以下操作,我已经粘贴了有关如何连接和登录到 ejabberd 服务器的代码,因为没有它,代码对于记住我的功能看起来不完整。我添加了解释如何在 SharedPrefernces 中存储和检索值的注释。
public class Login extends Activity
{
private XMPPConnection connection;
private ProgressBar progressBar;
private EditText userId;
private EditText password;
private CheckBox rememberMe;
private SharedPreferences sharedPreferences;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userId = (EditText) findViewById(R.id.user_id);
password = (EditText) findViewById(R.id.password);
rememberMe = (CheckBox) findViewById(R.id.remember_me);
progressBar = (ProgressBar) findViewById(R.id.login_progress);
//Retrieving SharedPreferences
sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
//Retrieving editor
editor = sharedPreferences.edit();
//Retrieving remember me checkbox's set value which if was stored in SharedPreferences
if(sharedPreferences.getBoolean(getString(R.string.remember_me), false))
{
String uId, pass;
uId = sharedPreferences.getString(getString(R.string.user_id), "");
pass = sharedPreferences.getString(getString(R.string.password), "");
userId.setText(uId);
password.setText(pass);
rememberMe.setChecked(sharedPreferences.getBoolean(getString(R.string.remember_me), false));
new Connection().execute(uId, pass);
}
}
public void login(View view)
{
new Connection().execute(userId.getText().toString(), password.getText().toString());
}
private class Connection extends AsyncTask<String, Void, Integer>
{
private static final int CONNECTION_FAILURE = 0;
private static final int LOGIN_FAILURE = 1;
private static final int SUCCESS = 2;
protected void onPreExecute()
{
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected Integer doInBackground(String... strings)
{
ConnectionConfiguration conConfig = new ConnectionConfiguration("192.168.1.66", 5222, "domain");
connection = new XMPPConnection(conConfig);
try
{
connection.connect();
Log.i("TESTING", "CONNECTED TO " + connection.getHost());
}
catch(Exception e)
{
Log.e("TESTING", e.getMessage());
return CONNECTION_FAILURE;
}
try
{
connection.login(strings[0], strings[1]);
Log.i("TESTING", "LOGGED IN AS " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
}
catch(Exception e)
{
Log.e("TESTING", e.getMessage());
return LOGIN_FAILURE;
}
return SUCCESS;
}
protected void onPostExecute(Integer integer)
{
progressBar.setVisibility(ProgressBar.GONE);
switch(integer)
{
case CONNECTION_FAILURE:
Toast.makeText(getApplicationContext(), "Failed to connect to the server.", Toast.LENGTH_LONG).show();
break;
case LOGIN_FAILURE:
Toast.makeText(getApplicationContext(), "Please check your user id and or password.", Toast.LENGTH_LONG).show();
break;
case SUCCESS:
if(rememberMe.isChecked())
{
//Setting value in SharedPrefernces using editor.
editor.putBoolean(getString(R.string.remember_me), rememberMe.isChecked());
editor.putString(getString(R.string.user_id), userId.getText().toString());
editor.putString(getString(R.string.password), password.getText().toString());
//Committing the changes.
editor.commit();
}
else
//Clearing SharedPreferences.
sharedPreferences.edit().clear().commit();
startActivity(new Intent(getApplicationContext(), Home.class));
}
}
}
}