android - Progress Dialog not show on screen -
i edited code according dear mayank'answer not show message sended input in displaymsg() method before method begines..i should methodtest() started nfc , in method onnewintent(intent intent)
@override protected void onnewintent(intent intent) { methodtest(); .............. } public void methodtest() { displaymsg("method 1 running"); method1(); displaymsg("method 2 running"); method2(); displaymsg("method 3 running"); method3(); } private int displaymsg(string msg) { totalmsg += msg; displaymsgclass dc = new displaymsgclass(); dc.doinbackground(totalmsg); } private class displaymsgclass extends asynctask<string, integer, string> { @override protected void onpreexecute() { textview.settext("hello !!!"); progressbar = (progressbar) findviewbyid(r.id.progressbar1); progressbar.setvisibility(view.visible); super.onpreexecute(); } @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); } @override protected string doinbackground(string... messages) { return messages[0]; } @override protected void onpostexecute(string result) { progressbar.setvisibility(view.invisible); textview.settext(result); } }
in layout:
<linearlayout> <progressbar android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:id="@+id/progressbar1" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textv1" android:hint="appletpass" android:gravity="center"/> </linearlayout>
remember asynctasks should ideally used short operations (a few seconds @ most.)
try learn more asynctask , there many mistakes in code
do not call
doinbackground()
manually.dc.doinbackground(totalmsg);
// errordisplaymsg() called several times, each time new instance of class displaymsgclass created
displaymsgclass dc = new displaymsgclass();
// erroronpreexecute()
textview.settext("hello !!!");
// nullpointerexception.textview.settext()
called without initializing it.
caution
do not call asynctask.execute()
more 1 on same intance.
eg:
displaymsgclass displaymsgclass = new displaymsgclass(); displaymsgclass.execute(); displaymsgclass.execute(); //error, illegalstateexception
will show basic demo based on implementation , can modify according own way.
public void methodtest() { // execute task new displaymsgclass().execute("download now"); } /* public void methodtest() { displaymsg("method 1 running"); method1(); displaymsg("method 2 running"); method2(); displaymsg("method 3 running"); method3(); } private int displaymsg(string msg) { totalmsg += msg; displaymsgclass dc = new displaymsgclass(); dc.doinbackground(totalmsg); } */ private class displaymsgclass extends asynctask<string, integer, string> { @override protected void onpreexecute() { super.onpreexecute(); // retrieve widgets progressbar = (progressbar) findviewbyid(r.id.progressbar1); textview = (textview) findviewbyid(r.id.textv1); textview.settext("download initialized"); progressbar.setvisibility(view.visible); } @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); } @override protected string doinbackground(string... messages) { // read commands string command = messages[0]; try { thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } return "download completed"; } @override protected void onpostexecute(string result) { //invoked on ui thread after background computation finishes progressbar.setvisibility(view.invisible); textview.settext(result); } }
Comments
Post a Comment