Android: Progress Dialog with AsyncTask - android

Since the game requires TTS, and need quite a long time to load, I would like to implement Progress Dialog (PD), as either in the following ways:
Implement AsyncTask in Game Index Page:
This will show the PD, but the PD is freezed, i.e. the looping circle inside the PD is not looping.
buttonC.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
new initialize_game().execute();
}
});
private class initialize_game extends AsyncTask<String,Integer,String>
{
#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();
}
}
}
AsyncTask for TTS:
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.
private class MainFrameTask extends AsyncTask<String,Integer,String> 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); // <====== 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 >= loveArray.length)
{
lastUtterance = 0;
}
Log.v(TAG, "the begin utterance is " + lastUtterance);
for(int i = lastUtterance; i < 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);
}
}
Question:
I found that the Progress Dialog does not show out when Button C in the game index is pressed. However, when the Game_star_intro is finally loaded, the progress dialog pops up for a very very short time and then gone.
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.
In this way, I have also tried to put the load TTS in AsyncTask inside Game_star_intro, yet the result is the same: the dialog just pops up for a millisecond.
Actually how should the AsyncTask be coded?? I have followed some website like this http://karanbalkar.com/2012/10/tutorial-5-custom-progressdialog-with-asynctask/
Thanks for your time!

You shouldn't start your activity in background thread. Start it, for example, in your onClick() method. You should put only your expensive code in doInBackground(), separating it from the framework lifecycle stuff. I guess you should implement AsyncTask inside Game_star_intro class for this.
ProgressDialog is not showing probably because UI thread is freezed until the work is done.
Also, naming conventions in Java suggest name classes without underscores, i.e. GameStarIntro :)

If I understood correctly from your question, loading of the Game_star_intro activity takes a lot of time because you use TTS in creation of this activity. The code you use is wrong. ProgressDialog from Index_game won't be shown when another activity is running (or is being created). You should use AsyncTask in Game_star_intro activity and use TTS there. In Index_game just start Game_star_intro:
buttonC.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Index_game.this, Game_star_intro.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
}
});
And in Game_star_intro something like this
public void onCreate() {
...
new TTLTask().execute();
}
private class TTLTask extends AsyncTask<Void, Void, Void> {
#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)
{
//... TTS code
return null;
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Log.i("result","" +result);
if(dialog.isShown())
{
dialog.dismiss();
}
}
}

Related

Asynchronous Task Method Calling is Very Slow in android

I've an Asynchronous task method, that calls background process. when i call this summaryCalc method, preexecute method runs when this method calls but doInBackground method takes more than 20 seconds to start. it takes a long time. is there any other way to improve the speed of calling doInBackground method or any other fastest way to execute thread? Thank you.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summary_date_select);
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
summaryCalc();
}
});
}
/**
* method to create asynchronous task to realign summary data
*/
public void summaryCalc() {
new AsyncTask<Void, Void, String>() {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(SummaryDateSelectActivity.this);
dialog.setTitle(getResources().getString(R.string.app_name));
dialog.setMessage(getResources().getString(R.string.please_wait));
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected String doInBackground(Void... params) {
ExtraSettingsDS settingsDS = new ExtraSettingsDS(getApplicationContext());
ExtraSettingsDO settingsDO = settingsDS.getExtraSettingsValues();
WeeklySummaryRecovery summaryRecovery = new WeeklySummaryRecovery(getApplicationContext());
/*Insert missing account order data*/
summaryRecovery.insertMissingAccOrderData();
if (settingsDO.getAccManage() == 0) {
summaryRecovery.summaryInsertForSeparateAccManage();
} else {
summaryRecovery.summaryInsertForJoinAccManage();
}
settingsDS.updateWeeklyFinishedDate();
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
dialog.dismiss();
intent = new Intent(getApplicationContext(), SummaryDetailsShowActivity.class);
intent.putExtra(KandhaConstants.IE_NEXT_ACTIVITY, accCheck);
intent.putExtra(KandhaConstants.IE_DAY_OF_LINE, currentDay);
intent.putExtra(KandhaConstants.IE_START_DATE, date);
startActivity(intent);
finish();
}
}.execute(null, null, null);
}
It is possible you have a lot of async tasks running. Calling .execute() will execute them one by one. Try calling .executeOnExecutor() instead.
http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask in Android starts but does not process

I know that there are many similar AsyncTask questions already, but in my case something is very unusual or am I missing something !?
As the AsyncTask is not allowed to run more than once. I call it with new B().execute(); so it should create a separated instance on each run !? Right?
The problem is after the B class is created and executed once, it wont work the second time the user calls the startBClass() method (just opens the dialog, but the actual work is not happening).
I just Debugged the code and realize that after the Dialog is closed, the Thread is still running in the background. What is the proper way to stop the background thread when the Dialog is closing? - And since I'm closing the first Dialog inside B class and create another instance of the B class, why is the second one not working? Can't multiple AsyncTasks run in parallel !?
I simplified the classes for easier understanding what I'm trying:
public class A {
/* Is called when user clicks a button */
private void startBClass() {
new B().execute();
}
/* Opens a Dialog with a countdown TextView (works first call only) */
private class B extends AsyncTask<Void, Integer, Void> {
private int secondsPassed = 0;
private double totalToPay = 0;
private Dialog dialog;
private TextView tvCost;
private Button dialogBtn;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new Dialog(ConfigurationActivity.this);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.setCanceledOnTouchOutside(false);
dialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
onPostExecute(null);
}
});
tvCost = (TextView) dialog.findViewById(R.id.textCounter);
dialogBtn = (Button) dialog.findViewById(R.id.button1);
dialogBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
while(true){
publishProgress(secondsPassed++);
SystemClock.sleep(1000);
}
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
totalToPay = 12.00;
tvCost.setText(totalToPay + " USD");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
final AlertDialog alert = new AlertDialog.Builder(ConfigurationActivity.this).create();
alert.setTitle("Information");
alert.setMessage("You should pay about " + totalToPay + " USD.");
alert.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface alertDialog, int which) {
alertDialog.dismiss();
}
});
alert.show();
}
}
}
dialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
onPostExecute(null);
}
});
This is no good. Per the docs:
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
To end it, I'd change both the code above, and the while loop in doInBackground().
protected Void doInBackground(Void... arg0) {
while(running){
publishProgress(secondsPassed++);
SystemClock.sleep(1000);
}
}
running is a boolean you set to true in onPreExecute(). Set it to false when you want to end it. Then your loop will exit and onPostExecute() will be called correctly.
Side note: Where is secondsPassed ever used?

progress Dialog simpel

i want to add a progress Dialog button when i click on this button before the new activity apperar, i think i don't need a thread, i did search but i find only that i need to do a thread and many other think it s not clear
i just want when i clik on a progress Dialog say to the user to wait so a few sec the other activity will appear that's all:
btn_newsfeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), CustomizedListView.class);
startActivity(i);
}
});
There are three different different ways in which you can use a ProgressDailog -using threads, handlers and async tasks.
here a example of async task for using a progress Dialog
private class Operation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params)
{
// code to be executed in background thread
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
progressDialog.show();
}
#Override
protected void onProgressUpdate(Void... values) {
// runs on UI thread and starts first
}
}

Progress bar doesn't update

I created a Progress bar but I can't see the loading animation. It's frozen. I want to display a progress bar when I click on the item and then see the bar working and not frozen. Here is my code:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (((TextView) view).getText().equals("Zman-New (rus)")){
progDailog = ProgressDialog.show(testLoading.this, "Getting data", "Loading...",true,true);
new GetDataTask("stringurl.xml").execute();
}
Here is the getdata
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
String url;
GetDataTask(String url){
this.url=url;
}
#Override
protected Integer doInBackground(Void... params) {
//do all your backgroundtasks
intent = new Intent(rusNewsP.testLoading.this, rusNewsTest.rusNewsActivite.class);
intent.putExtra("url",url);
startActivity(intent);
finish();
return 1;
}
#Override
protected void onPostExecute(Integer result) {
//finish up ( or close the progressbar )
//do something with the result
progDailog.dismiss();
super.onPostExecute(result);
}
}
If you just want to test Progress try this:
private class Initialize extends AsyncTask<Short, Short, Short> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
pd = new ProgressDialog(yourlass.this);
pd.setMessage("test");
pd.show();
super.onPreExecute();
}
#Override
protected Short doInBackground(Short... params) {
try {
synchronized (this) {
wait(2000);
}
} catch (InterruptedException ex) {
}
return null;
}
#Override
protected void onPostExecute(Short result) {
pd.dismiss();
super.onPostExecute(result);
}
}
And don't call startActivity in the doInBackground-Method. Call it in OnPostExecute instead. GUI Operations should not be done in doInBackground.
Try to start the Activity Direct from the UI thread as that will be fast.
Still,if you want this way then try to start it from the onPostExecute Method.
Not from the doInBackground.
protected void onPostExecute(Integer result)
{
//do something with the result
progDailog.dismiss();
intent=new Intent(rusNewsP.testLoading.this,rusNewsTest.rusNewsActivite.class);
intent.putExtra("url",url);
startActivity(intent);
finish();
}
And...don't call super.onPostExecute(result); after dismissing the progressDialog..after completing the doInBackground(Short... params),It will return directly to onPostExecute Method where it will dismiss the ProgressDialog first time and then execute the Constructor which will try again to dismiss the ProgressDialog which is already dismissed resulting into uncaught exception.

Android Async task porgress bar doesn't comes until background process is completed

I am building a project in which i use async task to show progress bar.
I am using get() method to wait the main thread so we can do the other task before .
but progress bar is showing after completion of doInBackground thered.
I Want to show the loading bar when the loading starts.
It will dismiss when onPostExecute calls.
public class TempConverterActivity extends Activity {
pojo p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b= (Button) findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showResult();
}
});
}
private void showResult() {
try {
new LoadData().execute().get();
} catch (Exception e) {
Log.e("async brix--", e.getMessage());
}
runned();
}
private void runned() {
ArrayList<String> al = p.getData();
for (String str : al){
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
private class LoadData extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(TempConverterActivity.this);
protected void onPreExecute() {
dialog.setMessage("Loading data...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected void onPostExecute(final Void unused) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
protected Void doInBackground(Void... params) {
p = new pojo();
new SoapParser(p);
return null;
}
}}
Please help . Thanks in advance.
You can try following code,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}
Edited: In my previous answer I suggested using a Handler; however, AsyncTask eliminates the need to do this which I didn't spot.
Why do you feel the need to call AsyncTask.get()? This is a blocking call, and you call this from the UI thread, thus it is ultimately a race condition as to whether it or onPreExecute() is run first.
I see no reason why you should call get() in this context. You want to call runned() after the AsyncTask completes, but you could do this by launching a new thread from onPostExecute(). Alternatively you could do as you do now, using get(), but call that from a new thread instead of the UI thread.

Categories

Resources