android - Using AsyncTask in PreferenceFragment -
i tring create sharedpreference save authentication info third party service. in preferences.xml there login , password fields check if values valid (authenticate) when edited. approach?
so far, have this:
on create
findpreference("sync_service_enabled").setonpreferencechangelistener(this); findpreference("sync_service_user").setonpreferencechangelistener(this); findpreference("sync_service_pwd").setonpreferencechangelistener(this);
my listener
public boolean onpreferencechange(preference preference, object newvalue) { if (preference.getkey().contains("sync_service")){ new authenticationremoteasynctask(this.getactivity(), user, password, service).execute(); } return true;
i need save token generated remote service need wait aynstask finished.
any sugestion?
i resolved problem criating custom dialogpreference. replaced positive button onclicklistener dont close dialog automatically , start remote task. asynctask notify preferencedialog after success , close 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 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 custom class wrapp async stuff , call notify method.
Comments
Post a Comment