Android Thread Launched from Button - android

I have been struggling to get a thread to launch and run in the background of my app.
My question is, How do I create a thread and launch it from a button? I'm sorry for creating this question but I have spent at least 5 hours with no progress at all.

your_button_id.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startThread();
}
});
create method
public void startThread()
{
Thread backgroundthread =new Thread( new Runnable()
{
public void run()
{
//Write your code that should be run on thread.Dont render UI here.
//render UI after thread in response handler like this...
responceHandler.sendEmptyMessage(0);
}});backgroundthread.start();
}
private Handler responceHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.what==0)
{
//Handle your UI here
}
}};

Related

Android handler.postDelayed is stopping my mediaplayer to run a song smothly?

hi i am new to Android programming i need little help in building a media player app in which i am using a seek bar to update the progress as below:
Handler handler = new Handler();
paly.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s_player.start();
p_bar.run();
}
});
Runnable p_bar = new Runnable() {
#Override
public void run() {
start_time = s_player.getCurrentPosition();
s_bar.setProgress((int) start_time);
handler.postDelayed(p_bar, 100);
}
};
so this code is updating sekkbar after 100ms, but the song is not playing smoothly???
I've spotted a mistake here, it might be related to the problem, might not.
Runnable p_bar = new Runnable() {
#Override
public void run() {
start_time = s_player.getCurrentPosition();
s_bar.setProgress((int) start_time);
handler.postDelayed(p_bar, 100);
}
};
in the p_bar you are calling the postDelayed within the run() method, while postDelay() will add the runnable object to the MessageQueue, and the run() method of the Runnable will be called by the System when this Runnable Object is reached in the Queue.
So it is like a loop, you can run, and postDelay will call run(), and run() will call postDelay() ....
So please don't call postDelay() in the run method.
Here is how i am doing;
paly.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s_player.start();
handler.postDelayed(p_bar,100);
}
});
Runnable p_bar = new Runnable() {
#Override
public void run() {
s_bar.setProgress((int) s_player.getCurrentPosition());
}
};
now the problem is its playing smothly but not updating progress bar
You have to implement seekbar listener....and this works for me
SeekBar.OnSeekBarChangeListener seekBarOnSeekChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
mediaPlayer.seekTo(progress);
seekBar.setProgress(progress);
}
}
};

Try to create stopper in my application

i created running app and i trying to create stopper that starting when user press start button.
but when i using thread the application return black screen
,this is my onCreate method i added thread
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
}
};
t.start();
the SetTextOnView method contain
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
while(true)
{
timedisp.setText(String.valueOf(total));
}
}
the problem start when i add the while statment
why is that?
Do the loop in your thread to not block the ui thread + add some delay to not block the ui thread :
Thread t = new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
Thread.sleep(1000);
}
}
};
And your SetTextOnView :
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
timedisp.setText(String.valueOf(total));
}

Spinner Progressbar in android

I want to implement ProgressBar in Android and when I execute the program, Progressbar should show for up to 2 seconds. I can't get it to work properly but I can't figure out why.
public void myThread(){
Thread th=new Thread(){
#Override
public void run(){
try
{
while(mRunning)
{
Thread.sleep(10L);//10s wait
YourCurrentActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
//DISMISS PROGRESS BAR HERE
mRunning=false;
}
});
}
}catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}
I have tried this but it does not giving me output as i want.
That what handlers are for in Android.
Example:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// cancel or dismiss your progressbar
}
}, 2000);

ProgressDialog won't dismiss in Thread

I am new to Android dev, and am trying to solve this problem that has been giving me some frustration. I am trying to close this progressDialog. When I run the app, it displays, the information is fetched, and the UI is updated. However, the dialog is never dismissed.
progDialog = ProgressDialog.show(HomeActivity.this, "", "Fetching info...", true, false);
new Thread(new Runnable() {
public void run() {
fetchInfomation(userID); //fetches information - Works
runOnUiThread(new Runnable() {
public void run() {
setLayoutList(); //updates UI - Works
progDialog.dismiss(); //doesn't seem to close progress dialog
firstView(); //displays prompt - Works
}
});
progDialog.dismiss(); //doesn't close dialog either
}
}).start();
Any ideas?
You can't interact with UI inside an external thread. There is some techniques to do that but it's not necessary.
You can use Handler.
For example:
...
private Handler mHandler;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mHandler = new Handler() {
#Override
public void handleMessage (Message msg) {
switch (msg.what) {
case 0: ...
case 1: ...
}
}
}
}
And:
....
new Thread() {
#Override
public void run() {
... // do your background jobs here
mHandler.sendEmptyMessage(...); // interact with UI
});
}
}.start();
It will be good practice if you do any GUI updates on UI thread. Inside any thread you can run your UI thread where you can do the UI stuffs or else you can go for message handler also which will do the same for you.
Runnable run_in_ui = new Runnable() {
#Override
public void run() {
// do your UI stuffs here
}
};
runOnUiThread(run_in_ui);

Android 2.2 Implements Runnable Public void run() not being called

I have the following:-
public class resApp extends MapActivity implements Runnable {
public void run() {
searchImage.setVisibility(View.GONE);
}
}
I also have a background thread that runs before this but that seems to run ok.
When i run the app the run() never gets called.
Can you help?
This code did work about 6 months ago but the device was 2.1.
Thanks
Chris
edit
I had already implemented
private Handler handler;
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.toString().equalsIgnoreCase("1")) {
ad.dismiss();
} else {
pd.dismiss();
}
}
};
as an example and I already have an asynchronous task that runs in the back ground and in 2.1 I could have getters and setters in there. I have now had to pull these out and put them into the run() method as 2.2 doesn't like setting onclicklistener in an async task.
All I need to do is call the run() method on post execute but have tried everything:-
protected void onPostExecute(Object result) {
// Pass the result data back to the main activity
if (dialog != null) {
resApp.this.dialog.dismiss();
}
}
Could I just do:-
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
this.resApp.run();
}
};
You can call the run() method by using Handler.
Handler myHandler = new Handler();
resApp myObj;
And call it by using myHandler.post(myObj);

Categories

Resources