How to call method every 30 second? - android

I am a new develop of Android. I want to create an Application about time. The question is "how to call method every 30 second?"
Example: Every 30 second the application will send some message.
I do not know how to do it firstly, I use this function
time = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if(isDistanceStable()){
Toast.makeText(ChangeStatus.this, "Your speed is normal.", 3).show();
}else{
Toast.makeText(ChangeStatus.this, "Your speed is abnormal.", 3).show();
callManualRed();
}
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
time.cancel();
//intervalCheckDistance();
}
}.start();
}
but how to call it every 30 second. Please give me an example or some solutions to solve it. Thank you very much and Sorry with my English

cdt = new CountDownTimer(30000, 30000) {
public void onTick(long millisUntilFinished) {
// Method
}
public void onFinish() {
cdt.start(); // Call Again After 30 seconds
}
}.start();
Remember to call cdt.cancel(); when you want to end the timer

Related

CountDownTimer not accurate

I have an Android service running in the background.
I want to be notified after a specific period of time (22 seconds), so I wrote:
private CountDownTimer mCountDownTimer = new CountDownTimer(22*1000,22*1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
doSomething();
}
};
I run this, and get the notification after 40 seconds, and even 50 seconds. Am I doing something wrong? How can this be done?
Actually you are putting interval time as wll 22000, what this you are doing wrong. Second parameter is the interval.So , Do this :
CountDownTimer alertTimer = new CountDownTimer(22*1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do here what you want
}
#Override
public void onFinish() {
}
}.start();

run function every x time in service Android

i am developing an app but i need run a function x minutes, i've tried using
scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new Runnable() {
#Override public void run() {
Toast.makeText(ser, "Servicio MyService update", Toast.LENGTH_LONG).show();
}
},
0,
100,
TimeUnit.MILLISECONDS );
}
but it does not work.
i use this function on a service, when the service is running it does'not call the function.
if I understand wrong please correct me,
you can use recursive func,
public void myfunc()
{
new CountDownTimer(70000, 1000) {
public void onTick(long millisUntilFinished) {
//
}
public void onFinish() {
Toast.makeText(getApplicationContext(), "7 seconds have passed",
Toast.LENGTH_LONG).show();//you can do here what you want every x(7)time
//you can put condition here to break recursive
myfunc(); //this calls again.
}
}
}

Stop watch in android withtimer starting from the top

I want a stop watch for an android quiz application but it should run from the top.I mean it should set the time for example 2 hrs for the application and then should come down from 2 hrs to 0 as the quiz moves on..How can we implement this in android????
Try to use countdown timer.
Refer the link.
http://developer.android.com/reference/android/os/CountDownTimer.html
Hope following code helps you out :)
final TextView textView=(TextView)findViewById(R.id.textView1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
textView.setText("done!");
}
}
.start();
}
});

Android countdown timer wont accept variable

i just started to learn creating android apps. I wanted to create a simple count down timer that takes a value from a edittext but countdown timer does not seem to run.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTxt = (TextView) findViewById(R.id.countDownView);
intervalTxt = (TextView) findViewById(R.id.intervalText);
findViewById(R.id.startBN).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
int testInt = 30;
//countDownTxt.setText(intervalTxt.getText());
int interval = Integer.parseInt(intervalTxt.getText().toString());
Log.d("buttonpressed", "interval for countdown is " + interval);
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
}
}
);
}
In particular, this program works only if i enter a numerical value such as 30000 in the 1st parameter of the CountDownTimer "cdt = new CountDownTimer(testInt, 1000)"
Can someone enlighten me please? Thank you!
"Doesn't work" how? You should post the error message you're getting or other symptoms of "doesn't work."
What's probably happening is CountDownTimer accepts only long values as the first parameter of its constructor. Not int values.
Change int testInt = 30 to long testLong = 10000.0f and see what happens.
The first parameter means milliseconds, by the way, so "30" isn't really going to get you much in the first place.
onTick() method is called in a separate Thread. But you don't have right to use setText() method outside the GUI Thread.
You must use a Handler object or Activity.postOnUiThread() method to execute something in the GUI Thread :
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
runOnUiThread(new Runnable() {
#Override
public void run() {
countDownTxt.setText("" + millisUntilFinished / 1000);
}
});
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
For more informations, read http://developer.android.com/guide/components/processes-and-threads.html#Threads

implement timer in my app

i am developing a game.i want to display score on button click.but it should be displayed only for few seconds.i want to implement timer in my app.but i dnt knw how to implement that.i searched in the google.but results were confusing me...given below is my code snippet.plz anybody help me...
OnClickListener clickball=new OnClickListener() {
#Override
public void onClick(View v) {
score=scorenumber.nextInt(9);
id=v.getId();
Log.v("", "u clicked me");
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
}
else if(id==R.id.ball3)
{
ball3.setText(Integer.toString(score));
}
else if(id==R.id.ball5)
{
ball5.setText(Integer.toString(score));
}
}
}
Something like
//Show score here
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//hide score here
}
}, 2000);
will hide your score after two seconds.
No need to go so deep in your case. Every view has postDelayed() method, that will run a custom code in UI thread after a set amount of time (in milliseconds). For example:
ball5.postDelayed(new Runnable(){
#Override
public void run() {
ball5.setText("");
}
}, 3000);
will clear "ball5" text after 3 seconds has passed
Another example
...
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
ball2.postDelayed(new Runnable(){
#Override
public void run() {
ball2.setText("");
}
}, 3000);
}
...
you can use CountDownTimer(); refer the documentation ,
Example :
new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
//hide your score here after 5 secondes (5000/1000)
}
}.start();
I think CountDownTimer is that what you need:http://developer.android.com/reference/android/os/CountDownTimer.html . It's quite simle for implementation
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Categories

Resources