GPS location update in background in android - 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!

Related

How to change default time delay 300 second ( Pjsua 2 Android) retry registration after registration failure?

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

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.

Android Fused Location Won't Deliver Periodic Updates

I want to get a hard location fix every certain minutes & a soft location fix every certain minutes and if user has moved more then certain meters. I been testing the logic with following piece of code walking (tried it with larger parameters as well while driving) but it doesn't really return me periodic location fixes. It would return a location fix right away when request starts then sometime return 1 location fix few minutes later that but then for up-to an hour it won't return a location fix.
LocationRequest locationRequest = LocationRequest.create();
int priority = PRIORITY_BALANCED_POWER_ACCURACY;
locationRequest.setPriority(priority);
locationRequest.setInterval(localInterval); //set to 6 minutes
locationRequest.setFastestInterval(localFastestInterval); //set to 3 minutes
locationRequest.setSmallestDisplacement(smallestDisplacement); //set to 10 meters
locationRequest.setNumUpdates(numUpdates); //set to Int.MAX_VALUE
locationRequest.setExpirationDuration(expirationDuration); // set to Long.MAX_VALUE
LocationServices.FusedLocationApi.requestLocationUpdates(locationClient, locationRequest, pendingIntent);
If I set displacement to 0 then I get periodic location updates. Any idea what is going on?
After long exhaustive testing & experimentation I've found that if you don't call setFastestInterval you will get periodic updates exactly according to the interval set with setInterval.
However as other applications can cause location fixes to be delivered very fast to you so just put a check for ignoring location fixes delivered faster than a certain threshold of time passed.
According to documentation: If setFastestInterval(long) is set slower than setInterval(long), then your effective fastest interval is setInterval(long) but that doesn’t happen: e.g. setting following parameters should give you a hard location fix every 1 minute but it does not (on Marshmallow at-least):
interval = 1 min
fastestInterval = 20 min
displacement = 0
If anyone can disprove my findings with a piece of code that would be great.

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.

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