Is there a timeout for Handler.postDelayed? - android

If I want to start some action from the service via Handler.postDelayed, can I start that action a couple of days ahead? Or there is some king of timeout for this, e.g. 12 hrs, 24hrs, etc.?
I am asking this as I think that the action does not execute if I set it for 24hrs ahead.
mHandler.postDelayed(this, 1000 * 60 * (24 * 60));
I am not sure about this and I just want to exclude this guess if the issue is not in it.

Handler is not intended for jobs to be executed 12 or 24 hours later. Use Timer instead.

Related

JobScheduler issues android

The jobscheduler runs every 10 mins periodically. What I have to do to run the schedule as soon as the app runs and keeps running periodically every 10 mins. What happens now is that: after the app is installed, it takes 10 mins to run the schedule. How to make it run initially and then repeats in every 10 mins? I have code for updating data to the server in onStartJob(). But the upload time is from 7 min to 25 mins too. I want to upload data every 10 mins but it varies randomly. Why is that?
JobInfo jobInfo =
new JobInfo.Builder(MYJOBID, jobService).setPeriodic(600000).
setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).
setRequiresCharging(false).
setRequiresDeviceIdle(false).
setPersisted(true).
setExtras(bundle).
build();
int jobId = jobScheduler.schedule(jobInfo);
if(jobScheduler.schedule(jobInfo)>0){
Toast.makeText(LiveTrack.this,
"Successfully scheduled job: " + jobId,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LiveTrack.this,
"RESULT_FAILURE: " + jobId,
Toast.LENGTH_SHORT).show();
}
.
public class MyJobService extends JobService {
#Override
public boolean onStartJob(JobParameters jobParameters) {
new MyDownloadTask().execute();
return false;
}
}
I want to upload data every 10 mins but it varies randomly. Why is that?
The JobScheduler API makes no promise of repeating at exact intervals.
But the upload time is from 7 min to 25 mins too.
According to setPeriodic() reference:
You have no control over when within this interval this job will be executed, only the guarantee that it will be executed at most once within this interval.
Still, we should be getting a callback at most 20 minutes apart. Let's look at JobInfo.Builder source code. Starting at setPeriodic(long):
public Builder setPeriodic(long intervalMillis) {
return setPeriodic(intervalMillis, intervalMillis);
}
Ok it calls it's overloaded cousin. Which says:
Specify that this job should recur with the provided interval and flex. The job can execute at any time in a window of flex length at the end of the period.
Wow so the flex length is also 10 minutes in our case? Not so fast:
/**
* Specify that this job should recur with the provided interval and flex. The job can
* execute at any time in a window of flex length at the end of the period.
* #param intervalMillis Millisecond interval for which this job will repeat. A minimum
* value of {#link #getMinPeriodMillis()} is enforced.
* #param flexMillis Millisecond flex for this job. Flex is clamped to be at least
* {#link #getMinFlexMillis()} or 5 percent of the period, whichever is
* higher.
*/
A minimum value of getMinPeriodMillis() is enforced.
:|
What is the minimum period you ask?
MIN_PERIOD_MILLIS = 15 * 60 * 1000L; // 15 minutes
So your call to setPeriodic(60000) doesn't accomplish anything. Minimum period remains clamped to 15 minutes.
JobScheduler is not really meant to be used for exact repeating periods. In fact it was built because a majority of the apps were abusing the AlarmManger api which provides this (exact repeating) functionality.

How do I start chronometer with a specific starting time?

Let's say I have this variable:
long myMillis = 20000;
This means that I want my Chronometer to start at exactly 20 seconds (00:20).
I tried doing this:
chronometer.setBase(myMillis);
But it doesn't work. It dosn't start with 20 seconds. It starts with some weird time that doesn't make sense.
In general the chronometer works like this (if you would like to set
the Base to a specific nr):
mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))
so make it:
mChronometer.setBase(SystemClock.elapsedRealtime() - (2* 60000 + 0 * 1000)))
For Kotlin,
To start Chronometer with starting time 20 seconds, you can use
val timeInMilSeconds = 20000
chronometer.base = SystemClock.elapsedRealtime() - timeInMilSeconds
chronometer.start()
This will start Chronometer with starting time 20 seconds i.e. 00:00:20
Its Late but may help others.
I have used following code in first fragment
chronometerTimer.setBase(SystemClock.elapsedRealtime());
chronometerTimer.start();
and then move on some condition to next fragment where chornometer should start at same time of previous chornometer ends, i get elapsed time using this code.
long elapsedMillis = SystemClock.elapsedRealtime() - chronometerTimer.getBase();
and i send elapsedMilis in next fragment and use following code
chronometerTimer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
chronometerTimer.start();
it worked perfectly.

GPS location update in background in android

I would like to keep updating my current location every 5 minutes to my server. I don't want to do anything if the current location is same every 5mins.
Is there any library that can do the above for me?
I looked into littlefluffy example it is does what I require but when I change timings instead of 1 min to 5mins. It doesn't seem to work at all.
In the sample code:
The class file that extends application has the following code:
LocationLibrary.initialiseLibrary(getBaseContext(), 60 * 1000, 2 * 60 * 1000, "mobi.littlefluffytoys.littlefluffytestclient");
Which is calls the GPS check every 1 min and force calls if there is no updates every 2 mins.
So I changed the timings to:
LocationLibrary.initialiseLibrary(getBaseContext(), 5 * 60 * 1000, 15 * 60 * 1000, "mobi.littlefluffytoys.littlefluffytestclient");
Call Gps location every 5 mins. But this calls every minute updates the same location everytime to server. Have anybody had the same problem?
Also:
https://code.google.com/p/little-fluffy-location-library/downloads/list?can=1&q=&colspec=Filename+Summary+Uploaded+ReleaseDate+Size+DownloadCount
it says deprecated?
So looking for library that can help my problem in android?
Thanks!

Doing a countdown properly

Each second that != the previous second a new value is posted and when the value reaches <1 the countdown finishes.
I note the starting time with System.currentTimeMillis() and simply calculate the remainder of the countdown from there. All this is done in a runnable which gets re-run over and over.
When typing out the min & seconds left I use this formula:
secondsLeft= (int) ((time / 1000) % 60);
minutesLeft = (int) (time / (60*1000));
The problem Im getting at is that when the secondsLeft reaches <1 the timer finishes. But my Alarm set through AlarmManager which uses pure millis and thus not rounding to nearest second, gets run a little sooner , or later than the timer finishes.
What can I do to make them synchronized?
Some extra info:
I am vibrating the phone when the countdown finishes. Hence I cant have the timer reach 0 and then the phone vibrating sooner/later.
I use AlarmManager for the vibrate as it wont go off if phone is asleep too long (service running in foreground works most of the time,but not 100%).
By your description, I believe you're getting bit by rounding. If you start the timer at, say, 1380866264454ms, next second by your algorithm happens in only 546 milliseconds, not 1000 - so your countdown would end approximately 454ms before your alarm is scheduled. Thus, get the last three digits of your start time and subtract them from the current time before you do the calculation you show in the post. The first change after (1380866264454 - 454) will be at (1380866265454 - 454), at least 1000ms away.

Android - Service, setup time

How to setup service to something (api request) everyday at particular time.
I dont know.
Right now I thing about two options:
1. Setup timer and every hour check the time and if it right, do a request.
2. setup the alarm, by alarmManager, but I dont know how to do it.
Another imported thing is the request must be a little random.
About 3-10 minutes, to prevent blocking the server by too many
request at the same time.
Take a look at this tutorial for scheduling events with an AlarmManager.
For the interval of 3-10 minutes you could just add something like
int rand = (int) (Math.random() * 1000 * 60 * 7 + 3 * 60 * 1000);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + rand, sender);

Categories

Resources