Why in Android Async task not working as it supposed to Do -
i have activity, splash activity
, want load 2 music in , when loading finish want start welcome page, unfortunately made wrong.
even though got log
made when load complete , got log when 2 music not loaded..
log
loaded 1 true loaded 2 true 1 not 2 two not 2
code
package com.syriatel.eattel; import android.app.activity; import android.content.intent; import android.media.audiomanager; import android.media.soundpool; import android.media.soundpool.onloadcompletelistener; import android.os.asynctask; import android.os.bundle; import android.util.log; public class splash extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.splash); new load1().execute(1); new load2().execute(2); } public int soundid, soundid2; private soundpool soundpool1, soundpool2; boolean isload1, isload2; class load2 extends asynctask<integer, integer, integer> { @override protected void onpostexecute(integer result) { super.onpostexecute(result); if (isload1 && isload2) { intent intent = new intent(splash.this, welcome.class); finish(); startactivity(intent); }else{ log.e("2", "not two"); } } @override protected integer doinbackground(integer... params) { soundpool2 = new soundpool(10, audiomanager.stream_music, 0); soundpool2.setonloadcompletelistener(new onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { isload2 = true; log.e("loaded 2", "true"); } }); soundid2 = soundpool2.load(getapplicationcontext(), r.raw.select, 1); return 1; } } class load1 extends asynctask<integer, integer, integer> { @override protected void onpostexecute(integer result) { super.onpostexecute(result); if (isload1 && isload2) { intent intent = new intent(splash.this, welcome.class); finish(); startactivity(intent); }else{ log.e("1", "not two"); } } @override protected integer doinbackground(integer... params) { soundpool1 = new soundpool(10, audiomanager.stream_music, 0); soundpool1.setonloadcompletelistener(new onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { isload1 = true; log.e("loaded 1", "true"); } }); soundid = soundpool1.load(getapplicationcontext(), r.raw.thip, 1); return 1; } } }
the problem creating separate thread within each asynctask
. when call
soundid = soundpool1.load(getapplicationcontext(), r.raw.thip, 1);
a separate thread starts loading music. however, asynctask
continue in thread, complete doinbackground
, go onpostexecute
, when gets there neither of flags set because soundpool1.load()
, soundpool2.load()` still running.
your code executing 1 expect!
to solve this, using code structure, need add code both doinbackground
methods. here number 2 updated:
protected integer doinbackground(integer... params) { soundpool2 = new soundpool(10, audiomanager.stream_music, 0); soundpool2.setonloadcompletelistener(new onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { isload2 = true; log.e("loaded 2", "true"); } }); soundid2 = soundpool2.load(getapplicationcontext(), r.raw.select, 1); // new code wait until load complete while(isload2 == false) { thread.sleep(1000); // 1 second (change feel fit) }
then onpostexecute
when music has loaded. wise make sure initialise flags, in each asynctask:
@override protected void onpreexecute() { isloadx = false }
Comments
Post a Comment