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.
Related
How can i change the default pjsua2 reregistration after registration failure. Currently it has been set to 300 second. I wish to set to retry registration after a registration failure to around 60 second.
i went through documentation ...but some how i am not able to implement them on sample android pjsua2 app.
unsigned timeoutSec Optional interval for registration, in seconds.
If the value is zero, default interval will be used
(PJSUA_REG_INTERVAL, 300 seconds).
unsigned retryIntervalSec Specify interval of auto registration retry
upon registration failure (including caused by transport problem), in
second.
Set to 0 to disable auto re-registration. Note that if the
registration retry occurs because of transport failure, the first
retry will be done after firstRetryIntervalSec seconds instead. Also
note that the interval will be randomized slightly by some seconds
(specified in reg_retry_random_interval) to avoid all clients
re-registering at the same time.
See also firstRetryIntervalSec and randomRetryIntervalSec settings.
Default: PJSUA_REG_RETRY_INTERVAL
link : https://www.pjsip.org/docs/book-latest/html/reference.html
Why you're unable to implement this? What's wrong when you are trying to?
Actually, you answerd youreself already. You should find initialization of AccountRegConfig object and set value of retryIntervalSec property.
AccountRegConfig regCfg = accCfg.getRegConfig();
regCfg.setRegistrarUri("sip:pjsip.org");
regCfg.setRetryIntervalSec(60);
account = app.addAcc(accCfg);
If this is not working, what misbehave you are seeing?
You can use bellow code, and read comments and set values depends on your needs.
accCfg = new AccountConfig();
/*
* Specify interval of auto registration retry upon registration failure
(including
* caused by transport problem), in second. Set to 0 to disable auto re-
registration.
* Note that if the registration retry occurs because of transport
failure, the first
* retry will be done after reg_first_retry_interval seconds instead. Also
note that
* the interval will be randomized slightly by some seconds (specified in
reg_retry_
* random_interval) to avoid all clients re-registering at the same time.
* */
accCfg.getRegConfig().setFirstRetryIntervalSec(3);
accCfg.getRegConfig().setRetryIntervalSec(10);
/*
* This specifies maximum randomized value to be added/subtracted to/from
the
* registration retry interval specified in reg_retry_interval and
* reg_first_retry_interval, in second. This is useful to avoid all
clients
* re-registering at the same time. For example, if the registration retry
interval
* is set to 100 seconds and this is set to 10 seconds, the actual
registration retry
* interval will be in the range of 90 to 110 seconds.
*/
accCfg.getRegConfig().setRandomRetryIntervalSec(7);
/*
* Optional interval for registration, in seconds. If the value is zero,
default
* interval will be used (PJSUA_REG_INTERVAL, 300 seconds).
*/
accCfg.getRegConfig().setTimeoutSec(65);
/*
* Specify the number of seconds to refresh the client registration before
the
* registration expires.
* Default: PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH, 5 seconds
*/
accCfg.getRegConfig().setDelayBeforeRefreshSec(10);
In my app I need to update user location by Geofire every 15 minutes continiously,but I am confused with some parameters.Currently I have
JobScheduler jobScheduler = (JobScheduler) getActivity().getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(AppConstants.USER_DATA_UPDATE_JOB_ID, new ComponentName(getActivity().getPackageName(), UserDataUpdateScheduler.class.getName()));
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPeriodic(900000);
builder.setPersisted(true);
jobScheduler.schedule(builder.build());
What exactly indicates jobScheduler to start job again builder.setPeriodic(900000); or jobFinished(params, true); ? And also can't understand the meanings of onStartJob and onStopJob return values.
Also an additional question,in Android O a background service cannot receive location updates more than a few times per hour.So approxiamtely how much is that few times?
setPeriodic(long intervalMillis)
Specify that this job should recur with the provided interval, not more than once per period.In others words, this job must repeat with the interval assigned, in milis, 9000 = 9 seconds.
JobFinished
Call this to inform the JobScheduler that the job has finished its work. When the system receives this message, it releases the wakelock being held for the job.
setPeriodic(long intervalMillis)
Specify that this job should recur with the provided interval, not more than once per period the MinimumInterval for this method is 15 minutes or 900000 milliseconds
And
JobFinished
call when your job is Finished
(if your task is short write on onStart method and return false without calling JobFinished method )
note: if you define Your Job Periodic in jobFinished always return false
Hello Guys
I want to create an alarm which run every second . I have searched many code but fond no solution , Please suggest some references .
Thanks
Amit Sharma
You can do this, create a CountDownTimer , say how long you want it to last for in the first param (in milliseconds), then set a period of time to run a piece of code in the second param. In the onTick() method, this is the code that will run in the interval specified in the second param, onFinish() is called when the period of the countdown is finished. Call the start() method on the CountDownTimer object when you want it to run.
int howLongTimerLastsInMilliseconds = 3000 // 3000 milliseconds
int tickEverySecond = 1000 // 1000 milliseconds == 1 second
CountDownTimer countDownTimer = new CountDownTimer(howLongTimerLastsInMilliseconds,tickEverySecond ) {
#Override
public void onTick(long millisUntilFinished) {
//do some work every second
}
#Override
public void onFinish() {
//do something when the time is up
}
};
countDownTimer.start();
According to the documentation:
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS
will shift alarms in order to minimize wakeups and battery use. There
are new APIs to support applications which need strict delivery
guarantees; see setWindow(int, long, long, PendingIntent) and
setExact(int, long, PendingIntent). Applications whose
targetSdkVersion is earlier than API 19 will continue to see the
previous behavior in which all alarms are delivered exactly when
requested.
Also this question was rised at code.googls.com and here is an explanation of it:
Suspiciously short interval 5000 millis; expanding to 60 seconds
This is working as intended, though is at present inadequately
documented (and we're aware of that side of the problem). Speaking
very generally: short-period and near-future alarms are startlingly
costly in battery; apps that require short-period or near-future work
should use other mechanisms to schedule their activity.
So, there is no inbuild way how to solve your issue using Alarm. And you should look out for another mechanisms or (very rough solution) use 60 independent alarms
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!
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);