In my app while copying files I want to ask user whether he wants to overwrite the file with same name, skip, etc. Here is some code from the thread:
if(target.exists()){
synchronized(this){
while(wait){
try{
popupHandler.sendMessage(popupHandler.obtainMessage(0, target));
wait();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Here is my handler:
Handler popupHandler = new Handler(){
#Override
public void handleMessage(Message m){
c.showConfirmDialog("The file " + ((File)m.obj).getName() + " exists. What do you want to do?",
"Overwrite", "Skip", "Keep both", Explorer.PASTE_CONFIRM, null);
}
};
After choosing an option from dialog something like that is called:
public void resume(){
synchronized(copyThread){
copyThread.wait = false;
copyThread.notify();
}
}
And here goes the problem. The copyThread on resume() is always null. I can't find what is wrong because the same scheme with pausing the thread works in my other class. It looks like the thread is dying after sending the message:/ Any ideas? Thanks in advance.
Related
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();
Let say I want to pass some configuration data before I start a thread so it can run with it right away how do I do that? Here is a simple code:
Here I'm trying to configure the sleep delay, but it requires me to declare data inside? How can I let the thread know?
public void startAniHandlerThread(){
int sleeptime;
Thread thread1 = new Thread(){
public void run(){
try {
sleep(sleeptime); Log.d("TEST", " Test ");
} catch (Exception e) {
e.printStackTrace();
Log.d("TEST", " "+e);
}
}
};
thread1.start();
}
You can declare sleeptime as
final int sleeptime = <value>;
That way you'll be able to access it from inside the Runnable.
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 need to display user info while app processing some data. I use ProgressDialog like
pd = ProgressDialog.show(MainActivity.this,
"", "Processing",
true, false);
Processing takes while connect to network and download data, but when network is not available it returns immediately and my progress dialog just flash very quick. Is there maybe more elegant solution then to put in catch Thread to sleep for some time :
try{
//connecting and calculating
}
catch(Exception exc){
Thread.sleep(400);
handler.sendEmptyMessage(0);
}
I need to set minimal time for my ProgressDialog regardless of success or failure ? Does anybody know how to achieve that ?
This will help you
Write the following code immediately after the dialog initialization
new Thread(){
public void run(){
try{
Thread.sleep(5000);
}
catch(Exception ex){}
try{
Message msg=actHandler.obtainMessage();
actHandler.sendMessage(msg);
}
catch(NullPointerException ex){
Log.e("Handler Exception :",ex.toString());
dialog.dismiss();
}
}
}.start();
actHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
doSomthing();
}
};
public void doSomething(){
dialog.dismiss();
}
I would do this the other way around: only show your progressDialog if you can establish the connexion. I feel that making the user wait 0.4 second for a result you already know just for the bling is pointless ;)