public void get(View view){
try {
asPt = new ProgressTask().execute(null,null,null);
Log.d("Watcher","Get finished");
}
catch (Exception e) {
e.printStackTrace();
Log.e("Watcher","Get Exception");
}
}
When I cancel(Boolean) the AsyncTask asPt the Line "Get finished" is never printed.
Why? It also doesn't catch an Exception in this method.
Remember cancel does nothing so you need to implement it yourself, see link: Android - Cancel AsyncTask Forcefully
Related
I am trying to save data into a Firebase RealtimeDatabase.
The process of saving is fine and works just well, but I was curious about an idea I had: To force the .setValue operation into a synchronous structure. I know, that that way isn't the best one, but I wanted to try it regardless of that fact.
So I came up with this code:
Thread t1 = new Thread(){
public void run(){
try {
Tasks.await(databaseReference.child("someChild").setValue(someObject));
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished");
}
In theory, that code snippet is intended to first set the value in the Database, then wait for the completion of that (through the Tasks.await()), and then print out **after that* the line "finished".
I wrapped the Tasks.await()-command in a second thread because when I don't, I get an exception which says something like "Tasks.await() must not be called in the main application thread".
But when I run the whole thing, it just gets stuck at t1.join();.
When I delete the t1.join();-statement, everything works just fine, but the second Thread (t1) isn't finished before the "finished"-output is there. I know why this is like that, but I am nontheless interested in a solution to that problem.
I'm working on an android app and I need to fetch data from internet.
I declare an "private Progress dialog" in Homepage.java.
In onCreate() method, call the function
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while...", true);
And create another thread to fetch the data, and in finally block, call
"dialog.dismiss()"
The whole onResume() code is shown in below.
#Override
protected void onResume(){
super.onResume();
Log.e(TAG, "onResume");
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while..", true);
/******Start fetching data.******/
Thread getDataThread = new Thread() {
#Override
public void run() {
try {
try {
getData();
} catch (JSONException e) {
alert_error();
e.printStackTrace();
} catch (IOException e) {
alert_error();
e.printStackTrace();
} finally {
dialog.dismiss();
}
} catch (SQLException e) {
alert_error();
e.printStackTrace();
} catch (ExecutionException e) {
alert_error();
e.printStackTrace();
} catch (InterruptedException e) {
alert_error();
e.printStackTrace();
}finally{
dialog.dismiss();
}
}
};
getDataThread.start();
}
At first, this worked great.(the spinner showed)
But at some point(I don't remember when), the dialog still works, but the spinner disappear
Show as below(place marked with red line should be the place where spinner display)
Spinner is gone
The dialog still works, but all ProgressDialog called anywhere from this app, the spinner is gone.(not even show up)
If I changed the getting data from internet part to sleeping for 10 secs
Which is shown below
#Override
protected void onResume(){
super.onResume();
Log.e(TAG, "onResume");
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while...", true);
/******Start fetching data.******/
Thread getDataThread = new Thread() {
#Override
public void run() {
try {
sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
dialog.dismiss;
}
}
};
getDataThread.start();
}
The spinner still doesn't show up.
I think that might be something related to global settings or values in my project, I create a empty activity with only one method other than super.onCreate and setContentView
I called
"ProgressDialog dialog = ProgressDialog.show(Homepage.this, "123", "456", true);"
and the spinner still doesn't show up!
But if I create a new project and added the same code, it works!
Thanks in advance!
I think the problem is in your phone setting, I had the same issue once, what you need to do is:
Go to developer options in your phone setting.
Turn transition on (in my case i had them turned off to make the phone act fast)
Restart the app and hopefully spinner will be there.
UPDATE:
Or you can try a library like Material Dialogs for that purpose, its one of my favorites, all you need to do is
Compile
compile 'com.afollestad.material-dialogs:core:0.8.6.0'
and then where you want to show the dialog
new MaterialDialog.Builder(this)
.title(R.string.progress_dialog)
.content(R.string.please_wait)
.progress(true, 0)
.show();
you can find more info here to customize it further and usage details.
Hope it helps
I have following code in which Progress Dialog do not shows.
#Override
public void onCompletion(MediaPlayer arg0) {
TrackAnalysis a = null;
File file = new File(songs.get(index).path);
final Track track;
try {
track = echoNest.uploadTrack(file, true);
final ProgressDialog dialog2 = ProgressDialog.show(MainGameActivity.this, "Analyzing...", "Please wait...",
true);
dialog2.setCancelable(true);
new Thread(new Runnable(){
public void run(){
try{
Thread.sleep(3000);
}
catch (Exception e){
e.printStackTrace();
}
dialog2.dismiss();
}
}).start();
track.waitForAnalysis(30000);
a = track.getAnalysis();
Log.i("TUTAJ", a.getTempo().toString() + track.getArtistName() + track.getTitle());
} catch (EchoNestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
OnCompletion method is from overloaded MediaPlayer.OnCompletionListener.
My question is why progress bar do not shows? When I placed ProgressBar code to onClicked method, then it works fine.
Check if you are calling start on media player before you set onCompletionlistener.
on clicked method does not require media player to start before its set.
You need to run the UI related code on main thread. You could either use runOnUiThread or make use of Handler. There are many examples available in the API Guides section of Android Developers site. Please take a look at Processes and Threads for a start.
I have a quit() method that I am using. I use a toast to print out "thank you for using this app". On the next line I do a system.exit(0); and when I run the app, the toast doesn't show before the system exits the app. Is there a way to fix this?
The way you should do it, in your activity:
Toast.makeText(this,"Thanks for using the app",Toast.LENGTH_SHORT).show();
finish();
Forget System.exit
You could run a thread that waits for the duration of your Toast.-
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(WAIT_TIME_IN_MILLIS);
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
I'm working on a service and I had the following code in onStartCommand:
try {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Networker.get("http://google.com/");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.run();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Even though the networking code is running in another thread, my service still crashed with the NetworkOnMainThreadException. The equivalent AsyncTask code does not cause this error. Is it because of the join?
I was doing this because I only need my service to run a single request and then quit, so I wanted to use the code above and return START_NON_STICKY to make the service close quickly again. Am I supposed to be doing something else instead?
Call start() and not run() on the thread to start the thread.
run() will just run your Runnable in the current thread.