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.
Related
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 wants to create a custome Input Method with word suggestions from a webservice in an asynchronous way. If it is not asysnchronouse , phone get stuck while connecting to internet. If I use Thread it cause an excpetion "ui can be touch only by the tread created ". I don't know runOnUIthread can be used or how. I understood that runOnUiThread activity method. Anybody please help. I used android Example app softkeybord.
I'm not sure I understand,
If the definition of the Thread is inside the Activity,
You can just call:
new Thread() {
public void run() {
while (i++ < 1000) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
btn.setText("#" + i);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
so I am building an experiment app where the background will change colour at random intervals.
I am stuck on the background change.
I have working code that changes the background colour, but when I put it in a thread/ try and catch bracket, the application is forced to close and doesnt give me an error?
Here is the code that works when used in the oncreate method:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.RED);
But when I want to make it "sleep" for 1 second and then change it to red, it bombs out.
Please note that this method is a separate method from the oncreate and is called from within there and will not work for some reason?
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
v.setBackgroundColor(Color.RED);
}
}
};
timer.start();
}
What am I doing wrong?
What I need:
when the app starts, it must wait for 1 second and then change the background colour without it bombing out.
Thanks in advance!
You cannot access UI thread from your custom thread. You have to run your runnable in UI thread. Change your changeBackground method to the following,
public void changeBackground(final View v) {
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
v.setBackgroundColor(Color.RED);
}
}
};
this.runOnUiThread(timer);
}
Or you can use Asynctask, this handles that issue for you. Here, and here.
Manipulate view state on UI thread:
v.post(new Runnable() {
#Override
public void run() {
v.setBackgroundColor(Color.RED);
}
});
More about it: http://developer.android.com/guide/components/processes-and-threads.html
UI operations can't be done in a Thread
v.setBackgroundColor(Color.RED);
Remove above line from finally and Use a runOnUIthread() to update UI in Finally.
and your final code will look like this
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.RED);
}
});
}
}
};
timer.start();
}
Solutions to fix this issue:-
Update you App
Clear Gmail Storage Data
Clear App Cache and Data
Reset App Preferences
Reset Smartphone Factory Settings
I found these steps with the help of YouTube.
Here is that link:-
youtube.com/watch?v=fx8Fv8RXag8
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
Sorry, I am new to Android programming. I have an xml parser that I don't want running in the UI thread. I want to display a progressdialog while the parser is running in its own thread but I don't want the main thread to start the next Activity until the parser is finished. I have code but the progressdialog doesn't display for the full duration. In place of the while loop, i've tried parserThread.join() with the same results. Also, I want to avoid timing out the UI thread. Any help is appreciated.
My method that invokes the thread, followed by the class that implements Runnable:
private void parseGasStationData() {
gasStations = null;
StationParser sp = new StationParser(activity);
Thread parserThread = new Thread(sp);
parserThread.start();
while (parserThread.isAlive()) {
// do nothing
}
gasStations = sp.getList();
Log.v("Parser-Status", "xml parsed successfully: "
+ (gasStations != null));
}
public class StationParser implements Runnable {
private Activity activity;
private final ProgressDialog pd;
public StationParser(Activity activity) {
this.activity = activity;
pd = ProgressDialog.show(activity, "", "Parsing data...", true, false);
}
#Override
public void run() {
try {
runParser();
} catch (XmlPullParserException e) {
Log.e("Parser-Error", "XmlPullParserException", e);
e.printStackTrace();
} catch (IOException e) {
Log.e("Parser-Error", "IOException", e);
e.printStackTrace();
} catch (ParseException e) {
Log.e("Parser-Error", "ParseException", e);
e.printStackTrace();
}
pd.dismiss();
}
In android we use the AsyncTask class to make background operations an show the results. See the main document page here the first example seems pretty useful for you, just change the doInBackground method for your parser.
This code:
while (parserThread.isAlive()) {
// do nothing
}
needs to go. As long as you have that, you might as well not be using a separate thread at all. (In fact, it's worse, because it's a "busy wait" that hogs the CPU.)
The right way to do this is with a call-back. When the parsing thread is done, it can call post(Runnable) to run a process on the UI thread that will make the progress dialog go away. You can use AsyncTask to help with this. See the guide topic Processes and Threads for more information.