Android - Service, setup time - android

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);

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.

Alarm initiated automatically at first time set a alarm

I am using alarm to start service at every 15 days once. Alarm is initiated at the first time of installation. Every 15 days once alarm trigger that service properly, but At the time of initiating alarm the service started.I dont want to start the service at the time of installation. how to stop this.
I dont whether i am using wrong format?
Note: Apart from this condition, I am not start service anywhere.
Intent intent = new Intent(context, InitiateService.class);
PendingIntent pIntent = PendingIntent.getService(context, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15 * AlarmManager.INTERVAL_DAY, pIntent);
this what i used in manifest file.
<service android:name=".service.InitiateService" />
this is my code of initiating alarm for starting service at every 15days once.
Guys If any one found any wrong in my question, Apologies me and Please correct those mistakes and answer me.
it is because you used System.currentTimeMillis()
try this
System.currentTimeMillis()*(15 * AlarmManager.INTERVAL_DAY)
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()*(15 * AlarmManager.INTERVAL_DAY), 15 * AlarmManager.INTERVAL_DAY, pIntent);
#Mehul Gajjar is correct about System.currentTimeMillis()
so you might wanna replace
System.currentTimeMillis()
with
(15 * 24 * 60 * 60 * 1000 * System.currentTimeMillis())
Explanation :
System.currentTimeMillis() will set Alarm to current time, so your service will start straight away. setting Alarm after 15 days will solve your problem.
15 * 24 * 60 * 60 * 1000 will be equals to 15 days. multiplying it with current time-miles will give you current time after 15 days.
in-short your code would be like
alarm.setRepeating(AlarmManager.RTC_WAKEUP, (15 * 24 * 60 * 60 * 1000 * System.currentTimeMillis()), 15 * AlarmManager.INTERVAL_DAY, pIntent);
I hope this will help you.

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!

Is there a timeout for Handler.postDelayed?

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.

Categories

Resources