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);
}
}
};
Related
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);
I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.
here is my code for runnable thread.
new Thread(new Runnable() {
public void run() {
while (seekbarStatus < 100) {
seekbarStatus = LoadingStatus();
// sleep 1 second to show the progress
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
seekbar.setProgress(seekbarStatus);
}
});
}
if (seekbarStatus >= 100) {
// sleep for 2 seconds, so that you can see the 100% of file download
runOnUiThread(new Runnable() {
#Override
public void run() {
if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
{
Ans_1.setEnabled(false);
Ans_2.setEnabled(false);
Ans_3.setEnabled(false);
Ans_1.setBackgroundColor(Color.GREEN);
points.setText("0 Punkt");
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
showDialogView();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
public void showDialogView() {
// TODO Auto-generated method stub
dialog.show();
}
});
}
}
}).start();
please help me. Any help would be appreciated.
I am not getting how to solve it.
Thanks in advance.
Just add an instance variable:
private boolean isCancelled = false;
Then in the button onClick:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isCancelled = true;
}
});
Then change
while (seekbarStatus < 100) {
to
while (seekbarStatus < 100 && !isCancelled) {
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
}
}};
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt;
TimerTask t;
stb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myt.mschedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Entering run");
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
}
});
//rg.getChildAt(cnt).setPressed(true);
}
},1000,2000);
I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...
You have to create the Handler in the UI Thread, i.e. in onCreate of your Activity.
Because you create it in the run method of a background thread, the handler will execute your code in that very same background thread.
You could also initialize your Handler directly:
public class MyActivity extends Activity{
private Handler handler = new Handler();
//more code
}
And then don't use runOnUIThread:
handler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
EDIT:
Ok try this cleaned up code. Because you did not post your full Activity this won't work out of the box:
public class TestActivity extends Activity {
private Button button;
static int cnt=0;
public ArrayList<RadioButton> buttonArray = new ArrayList<RadioButton>();
private Timer timer = new Timer();
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.schedule(new MyTimerTask(), 1000,2000);
}
});
}
private void doButtonStuff(){
buttonArray.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4){
cnt=0;
}
if(cnt>0){
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
}
private class MyTimerTask extends TimerTask{
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
doButtonStuff();
}
});
}
}
}
You can pass the Activity as a parameter to the method that runs the timertask, and then you can use Activity.runOnUiThread to execute your tasks in UI Thread. There are lots of post in stackoverflow site regarding the usage of runOnUiThread usage.
You don't need to call runOnUIThread inside the handler. By calling post on the Handler instance, the runnable you pass will be executed on the UI thread at some point in the future. Change your code to look like this and it should work:
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt;
TimerTask t;
stb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myt.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Entering run");
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
}
});
//rg.getChildAt(cnt).setPressed(true);
}
},1000,2000);
I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...
You can try to use your own Handler instead of Timer and timed taks.
RefreshHandler mHandler = new RefreshHandler();
With:
class RefreshHandler extends Handler
{
#Override
public void handleMessage(Message msg)
{
postYourElements();
}
public void sleep(long delayMillis)
{
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
And than use the function:
private void postYourElements()
{
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
mHandler.sleep(TimerIntervallInMs);
}
To start the Handler just call the postYourElements() function under onClick Method.
I'm not sure if this works for you but you can try.
youractivityname.this.runOnUiThread(new Runnable() {
public void run() {
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});