我想创建一个共享首选项以将一些身份验证信息保存到第三方服务。在我的preferences.xml中有一个登录和密码字段,但我想在编辑时检查值是否有效(验证)。什么是好方法?
到目前为止,我有这个:
on create
findPreference("sync_service_enabled").setOnPreferenceChangeListener(this);
findPreference("sync_service_user").setOnPreferenceChangeListener(this);
findPreference("sync_service_pwd").setOnPreferenceChangeListener(this);
我的听众
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().contains("sync_service")){
new AuthenticationRemoteAsyncTask(this.getActivity(), user, password, service).execute();
}
return true;
我还需要保存远程服务生成的令牌,所以我需要等待aynstask完成。
任何消化?
答案 0 :(得分:0)
I resolved my problem criating a custom dialogpreference. I Replaced the positive button onclicklistener so it dont close the dialog automatically and start my remote task. The asynctask will notify my preferencedialog after success and only then I close the dialog.
CODE
@Override
protected void showDialog(Bundle state) {
super.showDialog(state);
...
positiveButton.setOnClickListener(this);
negativeButton.setOnClickListener(this);
}
onclick handler
public void onClick(View view){
this.result = null;
if(view.getId() == positiveButton.getId()) {
String password = textPassword.getText().toString();
String user = textLogin.getText().toString();
new AuthenticationTask(getContext(),user, password)
.notify(this).execute();
}else{
alertDialog.dismiss();
}
}
check the result before close
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (result != null) {
SharedPreferences.Editor editor = getEditor();
editor.putString("sync_service_token",result);
editor.commit();
}
}
AuthenticationTask is a custom class that wrapp all the async stuff and call a notify method.