HI!
I want make service in OnCreate(), and every five minute, the service show notification..
can you show me about it??
thanks before :)
You can use the TimerTask class with the postDelayed method.
private TimerTask mTask = new TimerTask() {
#Override
public void run() {
//Whatever you want
postDelayed(this, REPEAT_INTERVAL); // rinse and repeat...
}
};
And in your OnCreate launching the TimerTask for first time:
postDelayed(mTask, INITIAL_DELAY);
You can find some information in this android article
http://developer.android.com/resources/articles/timed-ui-updates.html
Related
I want to Search Bluetooth Device Every three seconds.
so, I used Timer like this.
public void SearchingDevice() {
m_BTAdapter.startDiscovery();
m_timer = new Timer(true);
TimerTask timerTask = new TimerTask() {
public void run() {
m_BTAdapter.cancelDiscovery();
m_BTAdapter.startDiscovery();
}
};
m_timer.schedule(timerTask, 3000, 3000);
}
By the way, "android.bluetooth.adapter.action.DISCOVERY_FINISHED" Message
always printed twice..... why this message printed twice??
I used cancelDiscovery() only once...
please someone help me..!!
Thanks.
You should be careful with timer task. Maybe the problem is that you are not canceling task with activityLifecycle and each time you are creating a new one.
try:
#Override
protected void onPause() {
super.onPause();
m_timer.cancel();
}
can you paste the piece of code where you call SearchingDevice() method?
In my application i want to set a timeout when the user turn on 3G... after a certain amount of time elapsed , i will turn off 3G..
my problem is cancelling the scheduled timer.. every time i call timer.cancel() .. the program throws errors
the problem cause when i call clearTimeout() method..
Timer timer;
class RemindTask extends TimerTask {
public void run() {
//do something when time's up
log("timer","running the timertask..");//my custom log method
timer.cancel(); //Terminate the timer thread
}
}
public void setTimeout(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
}
public void clearTimeout(){
log("timer", "cancelling the timer task");//my custom log method
timer.cancel();
timer.purge();
}
please help me .. i am an android beginner..
Android has a class CountdownTimer which has start() and cancel().
I have this method
public void GetSMS(){
//in this method I read SMS in my app inbox,
//If have new SMS create notification
}
for this I think create timer tick method and every 5 sec call GetSMS()
How can I create a correct method for that ?
Here is an example of Timer and Timer Task. Hope this helps.
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
// Do whatever you want
}
});
}
};
timer.schedule(timerTask, 1000); // 1000 = 1 second.
Maybe with a timer and a timertask?
See javadocs:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html
Yet receiving broadcasts is probably a more solid solution.
See: Android - SMS Broadcast receiver
Use Timer.scheduleAtFixedRate() as follow:
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
GetSMS();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 5000, 5000); // every 5 seconds.
I saw it by accident.. This is not the right way to do it..
You don't need to check if there is a sms that received. Android provide broadcast receiver to get notified when sms is income.
Here you go, you have the link here.. Copy paste and it will work great
http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
Hope that this make sense
Although the above timer methods are the correct way to use timers of the sort you are after, I quite like this little hack:
new CountDownTimer(Long.MAX_VALUE, 5000)
{
public void onTick(long millisUntilFinished)
{
// do something every 5 seconds...
}
public void onFinish()
{
// finish off when we're all dead !
}
}.start();
Long.MAX_VALUE has, according the Java docs, a (signed) value of 2^63-1, which is around 292471 millennia ! So starting up one of these countdown timers effectively lasts forever relatively speaking. Of course this depends on your interval time. If you want a timer every 1 second the timer would "only" last 58494 millenia, but we don't need to worry about that in the grander scheme of things.
I have a runnable class like this:
public class GetUpdatesThread implements Runnable{
#Override
public void run() {
//call a webservice and parse response
}
}
Which I want fire every 10 seconds for instance...
I would like to know how can I manage handlers or runnables or timers in my activity to acomplish this?
Thanks in advance!
You can use TimerTask and can implement like this.
int delay = 5000; // delay for 5 sec.
int period = 10000; // repeat every 10 secs.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("repeating");
}
}, delay, period);
You can use the timer method called scheduleAtFixedRate from this link. I am already using it inside my project and it works like charm. You just have to give a starting delay time and a period for it then it works.
You can use Handler and calling the sendEmptyMessageDelayed method. Here's a tutorial or two on using Handler. Also check out Updating the UI from a Timer from the official doc - it covers both approaches with TimerTask and Handler.
The best way to do this thing is to use AlarmManager class.
1) schedule a AlarmManager with serRepeat method. link for AlarmManager
2) set Broadcast receiver in Alarmmanager, it will call Receiver every particular time duration, now from Receiver you can start your thread .
if you use Timer task and other scheduler, Android will kill them after some time.
I'm calling my TimerTask (m_timer) upon a button click:
m_timer.schedule(m_progressUpdater, 0, 500);
Which kicks off my run method:
#Override
public void run() {
//do some stuff
progressBar.setProgress(currentProgress);
if (progress >= 100) {
handler.post(new Runnable() {
#Override
public void run() {
CompleteTask();
}
});
}
}
I can call this once and it works perfectly. When I call it again, my app stops responding. I'm thinking that I need to cancel the task in my CompleteTask() method, but I've tried cancelling both the TimerTask and the Timer, and it still crashes. Anyone know what the problem might be?
Have you tried creating new TimerTask instance for the second call? And by the way, don't cancel the timer otherwise it will cancel all of its task. And what did the log say?
When you reschedule a Timer, it throws:
java.lang.IllegalStateException: TimerTask is scheduled already
It seems that you can only use a timer for once.
In order to reschedule a Timer, you need to simply create a new instance of it, each time. like the following:
// if you have already started a TimerTask,
// you must(?) terminate the timer before rescheduling it again.
if(m_timer != null)
m_timer.cancel();
m_timer = new Timer();
m_progressUpdater = new myTimerTask();
m_timer.schedule(m_progressUpdater, 0, 500);