android - AlertDialog setmessage not working inside Asynctask -
below code trying display progress in asyntask via onprogressupdate method aint showing in alert diaalog. initial message shown.
class downloadfilefromurl extends asynctask<string, string, string> { private alertdialog.builder alert; private int progress = 0; /** * before starting background thread show progress bar dialog */ @override protected void onpreexecute() { super.onpreexecute(); alert = new alertdialog.builder(context); alert.settitle("downloading.."); alert.setmessage("1"); alert.setcancelable(false); alert.show(); } /** * downloading file in background thread */ @override protected string doinbackground(string... f_url) { int count; try { url url = new url(f_url[0]); urlconnection conection = url.openconnection(); conection.connect(); // useful can show tipical 0-100% // progress bar int lenghtoffile = conection.getcontentlength(); // download file inputstream input = new bufferedinputstream(url.openstream(), 8192); // output stream outputstream output = new fileoutputstream(file); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... // after onprogressupdate called publishprogress("" + (int) ((total * 100) / lenghtoffile)); // writing data file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); } catch (exception e) { log.e("error: ", e.getmessage()); } return null; } /** * updating progress bar */ @override protected void onprogressupdate(string... progress) { log.d("myapp","progress :"+progress[0]); alert.setmessage(""+progress[0]); } /** * after completing background task dismiss progress dialog **/ @override protected void onpostexecute(string file_url) { // dismiss dialog after file downloaded } }
as have written log show progress update in onprogressupdate, log printed alert.setmessage in onprogressupdate seems not set message alert dialog.
as per code, alert
alertdialog.builder
, not alertdialog
itself. raised concern me because reason might not changing because showed builder, not give alertdialog
. tried out simple code:
public class mainactivity extends appcompatactivity { private alertdialog.builder alert; private alertdialog ad; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); alert = new alertdialog.builder(this); alert.settitle("downloading.."); alert.setmessage("1"); alert.setcancelable(false); ad = alert.show(); log.d("sample", "set message 2"); alert.setmessage("2"); log.d("sample", "set message 3"); ad.setmessage("3"); } }
at first, used alert.setmessage
(this alertdialog.builder
), , message did not change @ all. after putting in alertdialog
, setting message of alertdialog
instance, message changed. care try approach out. pass alertdialog.builder
alertdialog
first then, setmessage
using alertdialog
instance.
docs alertdialog , alertdialog.builder.
hope helps somehow. luck. :)
Comments
Post a Comment