Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Progress Dialog with AsyncTask
    primarykey
    data
    text
    <p>Since the game requires TTS, and need quite a long time to load, I would like to implement Progress Dialog (PD), as <strong>either</strong> in the following ways:</p> <h2>Implement AsyncTask in Game Index Page:</h2> <p>This will show the PD, but the PD is freezed, i.e. the looping circle inside the PD is not looping.</p> <pre><code> buttonC.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new initialize_game().execute(); } }); private class initialize_game extends AsyncTask&lt;String,Integer,String&gt; { @Override protected void onPreExecute() { dialog= new ProgressDialog(Index_game.this); dialog.setIndeterminate(true); dialog.setCancelable(false); dialog.setMessage("Loading!\nPlease wait..."); dialog.show(); } @Override protected String doInBackground(String... params) { buttonC.setBackgroundColor(getResources().getColor(R.color.tran_black)); Intent intent = new Intent(Index_game.this, Game_star_intro.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivityForResult(intent, 0); overridePendingTransition(0, 0); // 0 for no animation Index_game.this.finish(); return "Done!"; } protected void onPostExecute(String result) { super.onPostExecute(result); Log.i("result","" +result); if(result!=null) { dialog.dismiss(); } } } </code></pre> <h2>AsyncTask for TTS:</h2> <p>Once clicked from the Game Index Page, no PD is shown until the Game is loaded fully, and at that time then the PD pops up and off for a millisecond, i.e. even worse than that above.</p> <pre><code>private class MainFrameTask extends AsyncTask&lt;String,Integer,String&gt; implements OnInitListener, OnUtteranceCompletedListener { private Index_game_card_intro mainFrame = null; public MainFrameTask(Index_game_card_intro mainFrame) { this.mainFrame = mainFrame; } @Override protected void onCancelled() { stopProgressDialog(); super.onCancelled(); } @Override protected void onPreExecute() { startProgressDialog(); } @Override protected String doInBackground(String... params) { // setup TTS part 1.1 mTts = new TextToSpeech(Index_game_card_intro.this, this); // TextToSpeech.OnInitListener return "Done!"; } protected void onPostExecute(String result) { stopProgressDialog(); } // setup TTS part 2 @Override public void onUtteranceCompleted(String utteranceId) { Log.v(TAG, "Get completed message for the utteranceId " + utteranceId); lastUtterance = Integer.parseInt(utteranceId); } // setup TTS part 3 @Override public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { int result = mTts.setLanguage(Locale.US); // &lt;====== set speech location mTts.setSpeechRate((float) 0.8); mTts.setPitch(1.0f); if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { // button_header.setEnabled(false); } else { // button_header.setEnabled(true); mTts.setOnUtteranceCompletedListener(this); } } } } // setup TTS part 4 private void speakText() { lastUtterance++; if(lastUtterance &gt;= loveArray.length) { lastUtterance = 0; } Log.v(TAG, "the begin utterance is " + lastUtterance); for(int i = lastUtterance; i &lt; loveArray.length; i++) { params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i)); mTts.speak(loveArray[i], TextToSpeech.QUEUE_ADD, params); mTts.playSilence(ttsilience, TextToSpeech.QUEUE_ADD, null); } } </code></pre> <h2>Question:</h2> <p>I found that the Progress Dialog does not show out when Button C in the game index is pressed. However, when the <code>Game_star_intro</code> is finally loaded, the progress dialog pops up for a very very short time and then gone.</p> <p>I would like to show the ProgressDialog when it is loading up the game, not after the game is loaded then the dialog pops for a millisecond. </p> <p>In this way, I have also tried to put the load TTS in <code>AsyncTask</code> inside <code>Game_star_intro</code>, yet the result is the same: the dialog just pops up for a millisecond.</p> <p>Actually how should the AsyncTask be coded?? I have followed some website like this <a href="http://karanbalkar.com/2012/10/tutorial-5-custom-progressdialog-with-asynctask/" rel="nofollow">http://karanbalkar.com/2012/10/tutorial-5-custom-progressdialog-with-asynctask/</a></p> <p>Thanks for your time!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload