android - How can get timer from asynctask? -
cant access time in asynctask, variable point
doesn't change anymore
here code:
import android.app.activity; import android.content.intent; import android.graphics.drawable.bitmapdrawable; import android.media.mediaplayer; import android.os.asynctask; import android.os.bundle; import android.os.systemclock; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.progressbar; import android.widget.textview; import java.util.arraylist; import java.util.collections; import java.util.random; public class gameplayactivity extends activity { arraylist<player> arrplayer = new arraylist<player>(); textview tvlevel, tvpoint; imageview ivguessedimage; button btn1stchoice, btn2ndchoice, btn3rdchoice, btn4thchoice; int level = 1; int point = 0; random rand = new random(system.currenttimemillis()); progressbar customprogress; textview progressdisplay; int myprogress; mediaplayer mp1, mp2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_gameplayactivity); mp1 = mediaplayer.create(gameplayactivity.this, r.raw.uefa_champion_league); mp2 = mediaplayer.create(gameplayactivity.this, r.raw.the_time_of_our_life); mp1.start(); mp1.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mp2.start(); } }); mp2.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mp1.start(); } }); tvlevel = (textview) findviewbyid(r.id.tvlevel); tvpoint = (textview) findviewbyid(r.id.tvpoint); ivguessedimage = (imageview) findviewbyid(r.id.ivguessedimage); btn1stchoice = (button) findviewbyid(r.id.btn1stchoice); btn2ndchoice = (button) findviewbyid(r.id.btn2ndchoice); btn3rdchoice = (button) findviewbyid(r.id.btn3rdchoice); btn4thchoice = (button) findviewbyid(r.id.btn4thchoice); arrplayer = createplayer(); collections.shuffle(arrplayer); int resid = getresources().getidentifier(arrplayer.get(0).getplayerfile(), "drawable", getpackagename()); ivguessedimage.setimageresource(resid); btn1stchoice.settext(arrplayer.get(0).getplayername()); btn2ndchoice.settext(arrplayer.get(1).getplayername()); btn3rdchoice.settext(arrplayer.get(2).getplayername()); btn4thchoice.settext(arrplayer.get(3).getplayername()); customprogress = (progressbar)findviewbyid(r.id.customprogress); progressdisplay = (textview)findviewbyid(r.id.progressdisplay); new showcustomprogressbarasynctask().execute(); if(myprogress < 1){ intent intent = new intent(gameplayactivity.this, gameoveractivity.class); startactivity(intent); } btn1stchoice.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (btn1stchoice.gettext().tostring() == arrplayer.get(0).getplayername()) { final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.right_answer); mp.start(); point = point + 2; tvpoint.settext(point + " point"); createnewlevel(); myprogress = 100; } else { final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.wrong_answer); mp.start(); intent intent = new intent(gameplayactivity.this, gameoveractivity.class); intent.putextra("point", point); startactivity(intent); } } }); btn2ndchoice.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(btn2ndchoice.gettext().tostring() == arrplayer.get(0).getplayername()){ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.right_answer); mp.start(); point = point + 2; tvpoint.settext(point + " point"); createnewlevel(); myprogress = 100; } else{ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.wrong_answer); mp.start(); intent intent = new intent(gameplayactivity.this, gameoveractivity.class); intent.putextra("point", point); startactivity(intent); } } }); btn3rdchoice.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(btn3rdchoice.gettext().tostring() == arrplayer.get(0).getplayername()){ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.right_answer); mp.start(); point = point + 2; tvpoint.settext(point + " point"); createnewlevel(); myprogress = 100; } else{ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.wrong_answer); mp.start(); intent intent = new intent(gameplayactivity.this, gameoveractivity.class); intent.putextra("point", point); startactivity(intent); } } }); btn4thchoice.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(btn4thchoice.gettext().tostring() == arrplayer.get(0).getplayername()){ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.right_answer); mp.start(); point = point + 2; tvpoint.settext(point + " point"); createnewlevel(); myprogress = 100; } else{ final mediaplayer mp = mediaplayer.create(gameplayactivity.this, r.raw.wrong_answer); mp.start(); intent intent = new intent(gameplayactivity.this, gameoveractivity.class); intent.putextra("point", point); startactivity(intent); } } }); } public class showcustomprogressbarasynctask extends asynctask<void, integer, void> { @override protected void onpreexecute() { myprogress = 100; } @override protected void doinbackground(void... params) { while(myprogress>0){ myprogress--; publishprogress(myprogress); systemclock.sleep(200); } return null; } @override protected void onprogressupdate(integer... values) { customprogress.setprogress(values[0]); customprogress.setsecondaryprogress(values[0] + 1); progressdisplay.settext(string.valueof(myprogress)+"%"); } @override protected void onpostexecute(void result) { } } public void createnewlevel(){ arrplayer.remove(0); collections.shuffle(arrplayer); int randombuttonchoice = rand.nextint(4); int resid = getresources().getidentifier(arrplayer.get(0).getplayerfile(), "drawable", getpackagename()); ((bitmapdrawable)ivguessedimage.getdrawable()).getbitmap().recycle(); ivguessedimage.setimageresource(resid); switch (randombuttonchoice){ case 0: { btn1stchoice.settext(arrplayer.get(0).getplayername()); btn2ndchoice.settext(arrplayer.get(1).getplayername()); btn3rdchoice.settext(arrplayer.get(2).getplayername()); btn4thchoice.settext(arrplayer.get(3).getplayername()); break; } case 1: { btn1stchoice.settext(arrplayer.get(1).getplayername()); btn2ndchoice.settext(arrplayer.get(0).getplayername()); btn3rdchoice.settext(arrplayer.get(2).getplayername()); btn4thchoice.settext(arrplayer.get(3).getplayername()); break; } case 2: { btn1stchoice.settext(arrplayer.get(1).getplayername()); btn2ndchoice.settext(arrplayer.get(2).getplayername()); btn3rdchoice.settext(arrplayer.get(0).getplayername()); btn4thchoice.settext(arrplayer.get(3).getplayername()); break; } case 3: { btn1stchoice.settext(arrplayer.get(1).getplayername()); btn2ndchoice.settext(arrplayer.get(2).getplayername()); btn3rdchoice.settext(arrplayer.get(3).getplayername()); btn4thchoice.settext(arrplayer.get(0).getplayername()); break; } } level++; tvlevel.settext("level " + level); } } }
you interface
create interface in asynctask
, , create method used callback
public class showcustomprogressbarasynctask extends asynctask<void, integer, void> { interface progresslistner { public void onprogresschanged(int progress); } private progresslistner listner; public showcustomprogressbarasynctask(progresslistner listner) { this.listner = listner; } //..your remaining codes @override protected void onprogressupdate(integer... values) { //..your remaining codes listner.onprogresschanged(values[0]); } }
then implement interface in activity.
also pass instance of interface through constructor asynctask
.
then using instance can callback method pass progress value
public class gameplayactivity extends activity implements progresslistner{ //.. remaining codes new showcustomprogressbarasynctask(this).execute(); public void onprogresschanged(int progress) { = progress; } }
Comments
Post a Comment