Update Location after every 2 minutes on server android - java

Method to upload location to server every 2 minutes.
I am currently using Alarm Manager currently and some operating system close the Alarm Manager and the alarm donot ring after a particular interval of time. What is some other solutions that I can use?

Please read this article:
https://developer.android.com/topic/performance/scheduling.html#am
The AlarmManager API is another option that the framework provides for scheduling tasks. This API is useful in cases in which an app needs to post a notification or set off an alarm at a very specific time.
Location Manager - Android system offers location changes in runtime through location manager. Repeating updates can be achieved by using LocationManager#requestLocationUpdates

Related

Is there a better way to accurately use geofences without writing my own location request service?

Accord to Android's documentation on geofences, the geofence should trigger when the user enters, dwells, or exits the geofence with respect to the asked request.
BUT, testing the geofence feature myself, a geofence will only trigger if an app requests the user's location. Simply using the tutorials code and moving the device into the geofence WILL NOT trigger the geofence. I have to open Google maps or some other app that will request the user's location, which I assume notifies Google play services, which then fires my geofence. Is this really how are geofences are designed? Proof that this is how geofences work I found from this answer. (Note: I am also using a Broadcast Receiver like the accepted answer in that link. The receiver does not trigger unless a location request is made within the geofence.)
I don't want to have a persistent background service asking for the user's location every 5 minutes just to trigger the broadcast reciever. I've thought of listening for system intents such as the user turning the screen on, disconnect from wifi, etc that would fire a service I would make that asks for the user's location. But shouldn't geofences trigger natively without extra need from my app or other apps to update the user's location?
As mentioned in this answer https://stackoverflow.com/a/54436709/1505074 you need to change the sample Code to use a Broadcast Receiver instead of a Service. This way Geofences basically work but with some caveats. You have a latency depending on API-level, manufacturer and of course the device state. Often you will get the fence event when turning the device on. To really evaluate fence events you need to have a network logging service and do more complex analyses.
The problem with your own Geofence Trigger will be that you only get a real location update in the background about 4 times per hour if you are not using a foreground service which will drain battery. Without you will only get the last location even with the location update callback if you are in the background.
Transistorsoft has created a library to handle different ways of background location tracking and you will find many approaches there, but it is paid: https://github.com/transistorsoft/background-geolocation-lt
Just want to share my findings. After implementing geofence using the broadcast receiver way I found that it can be triggered without opening any location apps like Google maps given the criteria below:
Test on real device, I found that my geofence doesn't get triggered on the Android emulator even after waiting one hour moving out of geofence zone. I won't trust the emulator because of lack of sim card and real wifi so I don't know what's the missing pieces.
Wait 2 to 3 minutes if no other apps are requesting for location in the background. I tested this by denying location access to all other apps (or switch them to 'only allow when using app'), including the Google app on my Pixel phone.
I've been consistently getting geofence events in like 2 to 3 minutes, just like the docs says:
On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes.
For my case my app is not running on background, as it is not required for geofence to trigger if you're using broadcast receiver, I killed it on the recent apps page, or you can even use 'Force stop' in the apps info page, it doesn't matter.

bind service stops after 1 or 2 mins when phone is locked in some devices like oppo, one plus device

IN bound service when I lock the device after the 70- 80 seconds service stops I am testing on realme 3 pro and one plus 5 both having API 9 PIE. How to keep service running until the app is in recent activity. Even foreground services are also stopped. Thanks in advance.
Realme, Oneplus, Samsung, Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.
What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.
These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.
Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.
Conclusion: Visit https://dontkillmyapp.com and see how the apps are whitelisted for your OEM. Once whitelisted, it will work as expected.
But out there in real world, it will get killed and users won't manually whitelist it.
Custom Roms have it's own security apps and it close the app service when u remove it from recent or after few minutes.
To keep running your app in background you have to enable autoStart from settings
Refer this : How to fix the issue that the activity is killed automatically after some minutes in newly version as like vivo and oppo
Facing same issue from long time, Try to add FCM in your application and start service from it directly or else set alarm and startservice.
In some cases above solution work. i have added above solution in 4 project but succeed in only 1.
Issues which I have found while overcoming problem with firebase.
Firebase Notification coming to app but Background service not starting
Firebase Notification Not coming to application
Firebase Notification coming and service starting with Broadcast Receiver (Sometimes
service not starting directly from onMessageReceived() method of Firebase so i
decided to set broadcast Receiver ) but sometime its stop working.
Issue which I have found without implementing Firebase
If we set background service to run after every 5 mins than for 1-2 hours its
running on 5 mins interval but after 2 hours time gap increased and service runs on
15 mins of interval and in some devices its run on 20-30 mins of interval(i found
this in right time because phone may goes in ideal mode).
As mention in your question, sometimes any of above implementation doesn't work.
Tip : What I found in system alarm log ?
I have set alarm for 2019-08-08
11:07:00 which not fired and current time is 2019-08-08 11:10:00 but its showing in
pending in system alarm log.
You can check system all Alarm set by your applications
D:\SDK\platform-tools>adb shell dumpsys alarm

Push notification every day

How can I create a service which will push a notification at a specific time everyday? I've been trying many method on google but they did not work well. Anyone know a good way to do this? Someone say its better to use AlarmManager, while some others say its should be JobScheduler because AlarmManager is deprecated.
Anyone have a good reference?
If you want something to be executed on exact time then you should use AlarmManager.
Based on the documentation:
Standard AlarmManager alarms (including setExact() and setWindow())
are deferred to the next maintenance window.
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
You cannot trigger something at specific time using JobScheduler. The execution of job is under OS control. The jobs will be deferred during doze mode hence trigger at exact time won't be possible.
Use JobScheduler when you want to ensure that a Job must be triggered within specific interval however the execution at exact time is not crucial.
Set up a backend server for Push notification . When Api hit request to FCM for push request with device id then device will get notification. For Automatic push from backend , use cron from backend .A background process run in backend api and will hit FCM in some time interval and device will receive PUSH Notification.

Android alarm repeated every (less then) 1 sec

I'm trying to check what app is in foreground and launch my app if user locked that app. To do so I fire alarm, start service to check for active app and set alarm to fire in one socound from now. But somehow the alarm is allways delayed to 5 secounds..
This is how Im setting it:
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000, alarmIntent);
I've tried it on android 5.1 and also on 6.0. It's the same.
Do you know why it is delayed? or do you know about any better way to check for active app as soon as possible? because I think this way consume lot of battery.
Thanks.
So this is what I was looking for. Only disadvantage is that user has to enable accessibility service manualy from settings.
https://stackoverflow.com/a/27642535/4506191

How to send notification even if app is killed

I am working on an Android project that has a part which dose this:
1) the user enter a data from a data field and save it in a text file
2) the app should send notification even if the application is killed by the os, at that date, the one wrote by the user.
For example:
I write 31.01.2015
The app will notify my only on 31.01.2015 even if i don't open that app anymore.
The question is how do i have to do this?
Thanks!
It sounds like you want a notification to be posted to the notification bar.
If so I advise using an alarm.
However, chances are if this is days in the future, the phone may be shut off. So you should store when the alarm should go off, create a Broadcast receiver for the on boot complete event (this requires a permission), and re-setup the alarm when the boot is complete.
This should allow the notification to appear, independent of the apps life-cycle, as long as the app is not uninstalled.
Note: You will have to calculate the milliseconds between the date for the alarm, and the current time. Calendar should help.

Categories