java - How can I change the ActionBar color after AsyncTask? -
in app i'm performing asynctask
in onpagefinished
attached webview. method looks this:
@override public void onpagefinished(webview mywebview, string url) { new sendrequestasynctask().execute(); // when page has finished loading dismiss progress dialog if (progressdialog != null && progressdialog.isshowing()) { progressdialog.dismiss(); } }
and sendrequestasynctask
looks this:
public class sendrequestasynctask extends asynctask <void, void, void> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); //runs in ui thread } @override protected void doinbackground(void... params) { // todo auto-generated method stub httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("myscript.php"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("request", "12345")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); // writing response log httpentity resentity = response.getentity(); if (resentity != null) { string responsestr = entityutils.tostring(resentity).trim(); log.v(tag, "response: " + responsestr); string[] parts = responsestr.split(":"); parts[1] = parts[1].replace("\"", ""); parts[1] = parts[1].replace("}", ""); if (parts[1].equals("01")){ actionbar.setbackgrounddrawable(new colordrawable(color.parsecolor("#ffffff"))); } else { log.v(tag, "no success: " + parts[1]); } } } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); //runs in ui thread can update layout here } }
however, crashes app. guess i'll need change actionbar's color in onpagefinished
method. however, don't know how variable asynctask
onpagefinished
. furthermore, don't know how change color color @color/
xml file...
try this,
public class sendrequestasynctask extends asynctask <void, string, string> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); //runs in ui thread } @override protected string doinbackground(void... params) { // todo auto-generated method stub string strreturn = ""; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("myscript.php"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("request", "12345")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); // writing response log httpentity resentity = response.getentity(); if (resentity != null) { string responsestr = entityutils.tostring(resentity).trim(); log.v(tag, "response: " + responsestr); string[] parts = responsestr.split(":"); parts[1] = parts[1].replace("\"", ""); parts[1] = parts[1].replace("}", ""); strreturn = parts[1]; } } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } return strreturn; } @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); //runs in ui thread can update layout here if (result.equals("01")){ actionbar.setbackgrounddrawable(new colordrawable(color.parsecolor("#ffffff"))); } else { log.v(tag, "no success: " + parts[1]); } } }
Comments
Post a Comment