I want to set up a timer in an Android application that will call a function after every 15/30/45 and n minutes when user login. But also it will stop timer when user log off. and timer begin from start if user login again. I want that option(15/30/45/n miutes) to be saved in database so that I can update list after sync.
Is Timer a good approach or I need to use alarm services? Or is there any system services required?
Is it possible to change previous doc/file in local phone database storage to new doc that is receiving through web server? is there any system services required to do so?
Use following code to call your function every 15/30/45
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#SuppressWarnings("unchecked")
public void run() {
try {
"Your function call "
}
catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, "Timer value");
Related
How to run speech recognition in service continuously in offline, I have tried CMU but its not clearing hypothesis and reacted to every word which I spoken instead of specific command. Thanks those who help me!
I don't know if it's the best solution, but it may work :
private final Listener listener = new Listener();
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#SuppressWarnings("unchecked")
public void run() {
try {
if(!listener.isListening()){
listener.listenVoice();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 500);
You have to create an AsynchronousTask that you will repeat every X seconds. In my case, I did every 500ms. You have to check if the listener is listening everytime because you don't want an uncomplete sentence with missing words.
I know that this solution should strange, but there isn't a proper way to do it without some Google private function.
I want to load data from server synchronously and show it in a view like ListView or TextView without any pulling request each time. I need the data will be loaded if there is any changes in server.
Right now I am using asynctask to pull data from server but to load new data or update I need to do pulling request using a reload button.
I tried it using a thread which will run after a certain time. But I know there might have some better method obviously.
How can I do the same thing without the manual pulling request?
For example gmail app loads new emails when there is network access without any pulling request from user. I need the same thing in my Android Application.
Thanks in advance.
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}
The AsyncTask does run on a separate thread, but cannot be started from other threads than the UI thread. The handler is there for allowing that.
I need to repeat a task every 1 hour in the background (I am sending some info to my server).
I tried to use a service with a post delay handler calling it self.
handler = new Handler();
runable = new Runnable() {
#Override
public void run() {
try{
//sending info to server....
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable
handler.postDelayed(this, 1000*60*60);
}
}
};
handler.postDelayed(runable, 1000*60*60);
This did not work, in small time interval of 1 minutes it worked fine, when i changed it to 5 minutes it worked for about 5 repetitions and then the timing got wrong and after an hour the service shut down.
i want to try to use a AlarmManager but in the documentation
it says "As of Android 4.4 (API Level 19), all repeating alarms are inexact"
does anybody know how inexact it is? is it seconds? ,minutes?
can i rely on this to work on time?
does anybody have any other suggestions for repeating tasks in a service?
Thanks
You can use this This Code is for repeated calling on oncreate method or anyother thing
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
onCreate();
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 1000 ms
}
I would like to periodically check for updates doing network call every 30 sec and update listview accordingly, only if my screen is in foreground (thus not from service). What I am doing for that is -
private void refreshPeriodically()
{
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new callToMyAsyncTask().execute(context);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 30*1000); //execute in every 30 sec
}
but, that hangs my list while scrolling.
What should I do for that?
You shouldn't use handler. When you call handler.post() it will run on UI thread so your list will freeze. Start a new callToMyAsyncTask in run method in TimerTask without using handler and you can call timer.cancel() when your activity is not in foreground.
Also I think you should use scheduleAtFixedRate instead of schedule.
However I think you may have some problems without service. When you cancel timer it will not cancel current callToMyAsyncTask and when it call onPostExecute activity is no longer available. But I don't know it will crash or not.
I am using a timeschedular in android to schedule an AsyncTask in my app.I was able to work it out well,now i want every time the duration after which the task runs to be changed i am using this code
ip=3000;
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
play_duration=durat.get(vn);
ip=Integer.valueOf(play_duration);
ip=ShowImages.ip*1000;
Showtime s1 = new Showtime();
s1.execute(what,f12,transi,play_duration);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0,ip);
i am trying to change the value of ip variable every time in the thread but it do not change and i do not know why my thread always run after the initial value of ip that is 3000 miliseconds, can someone suggest a way to do it..thanks
to change component on the UI use the runOnUiThread,
see here, here and here to do that thing on the background you can use something like -
while(true)
{
runOnUiThread() {
do some stuf here;
}
Thread.Sleep(300); //<--- that should be nothing for the user and enough for the cpu
}
if you need more accurate answer post your code and ill guide you through