keeping an android service running for a long time - java

i want to run an android service for a very long time even when the phone is locked. i have tried job Intent Service and work manager but it doesn't work for long period.
the service is supposed to call an server API periodically.
work manager minimum interval is 15 minutes but i want to call this api every minute
is there any way to do that?

The best option you can use is Workmanager which is part of the Jetpack as in background it runs Jobscheduler or the Alaram manager according to api level.Now regarding long running service to run the service in background first thing you need to do is to show the notification to the user but in my case still it was stopping when device is locked or on sleep mode.So i that case WAKE_LOCK worked for me to awake the device.So i your can look whether it fits for your solution or not also make sure if you use wake lock then once your take finish don't forgot to release wake lock.

1) add Service.START_STICKY
Service is restarted if it gets terminated. It will tell the system to create the newest copy of the service when available memory is sufficient to do after it retains the state and recovers from the low memory.
2) add android:persistent="true"
<service android:name="com.myapplication.MyService" android:persistent="true"></service>
although, you can use WorkManager for persistent works

Since version 2.3.0, work manager has support for long running operations with foreground notification support.
https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running
As long as you can foreground api of worker, user will see a notification and during that time your worker can continue working in background.

Related

How can I make a background service for timer without showing a notification?

I have tried many solution but useless, all say use foreground service.
is there a way to trick android or to use a proper way.
i know it's restriction from android to inform the user that an app is draining the battery without using it.
The answer is use a foreground service. You can't have a background service for more than 2 minutes on modern Android.
Now if your timer is long (say 1 minute+) an alarm instead of a timer would work better. That wouldn't require a service, its a broadcast and your app would be restarted to handle it. Of course if you go into Doze those are still limited.

Huawei Y5 kill the foreground service after 10 minutes on locked mode

I have developed android app in which i am running foreground service and it fetches the user current location and send it to server after user changes its location or if user doesn't change its location then it pushes the current location every 5 minutes. Now the scenario is; on Huawei phones I've noticed issue. Whenever i lock my phone then the Huawei OS stops the foreground GPS service and location doesn't send to the server which creates issues for me. I've tried all the solutions available on stack-overflow but didn't succeeded an
This happens because of Doze mode, here you can read about Android Doze Mode https://developer.android.com/training/monitoring-device-state/doze-standby
and you can use Work Manager for your purpose: https://developer.android.com/topic/libraries/architecture/workmanager
WorkManager allows you to schedule work to run one- time or repeatedly using flexible scheduling windows. Work can be tagged and named as well, allowing you to schedule unique, replaceable, work and monitor or cancel groups of work together. Scheduled work is stored in an internally managed SQLite database and WorkManager takes care of ensuring that this work persists and is rescheduled across device reboots. In addition, WorkManager adheres to power-saving features and best practices like Doze mode, so you don’t have to worry about it.

Android kills my app after some time in background

I have an app that sends TCP messages. I need the messages to be sent as long as the app is alive. So, I granted access to battery optimization and added all the required wake_locks and everything seems to work fine even when screen is off.
The problem is that I noticed that every time that I leave my phone with the app in the background, when I come back after few hours(or less) no messages are being sent, and when I enter my app it loads as new instance and not like an app coming from background. What can I do to have my app not being killed by Android?
I guess that this is what happens
You need to use a foreground Service; a service started via startForeground().
The service does not "re-open" your activity; it is a component of your application that may be long-lived. You might prefer to think of it as an activity without any views.
Yes, as the others mention you have to use Service for background execution.
Additionally, also, keep in mind of the Background Execution Limits for Oreo and above.
You can also look at JobScheduler for managing asynchronous tasks efficiently.
Create a 1px*1px transparent view on the desktop, such as toast.(the Android os will level up your priority as a foreground process)
Suggest your user to add your app in the white list.(in some rom)
Always put your app`s notification in the notification bar.(also need a service)
create a guard process, when your app die, send a broadcast to guard and let guard restart your app. when guard die, send a broadcast to app and let app restart your guard.
Be careful of battery consumption and don`t trouble your users too much

Is it possible to have a count down timer continue running even after the user has closed the app

Im thinking of having an android app that has a count down timer. However, i need this timer to be running too even if the user kills the app and once the count down timer reaches 00:00:00, it triggers an action regardless of the application being active. Is there a possible solution to that?
Yes it is possible. Just run on background thread so your thread will continue to run even if app is closed.
Using service is a good idea but you have to work in background thread even in service. Because services work in foreground thread by default.
In your case I think the best way to do that is using AlarmManager. Try it.
In Both case (service and alarm) reboot will kill your thread. However you can recreate it. You can use services that will start again if they get killed. Also you can set a BroadCastReceiver that will be called after reboot is complete. So you can recreate your timer in onReceive method.
*
Yes it is possible. use a service , your service will continue to run even if app is closed.
you should call a service using AlarmManager with in a fix time .
*
using services will be the best option for your requirement. OnFinish of the timer, you can trigger your action.

How to schedule my android app to do something every hour

I want my app to access database every hour and read next record from the table then update desctop widget and send notification. I know that there is AlarmManager which I can use to register my Intents but they are deleted when the phone is turned off or rebooted.
Is there any other android class/service that I would update my application continuously even when I reboot my phone?
Thanks,
take a look at the demo applications provided with android sdk
http://developer.android.com/samples/RepeatingAlarm/index.html
the look at AlarmService_Service for the implementation of the service once the alarm has been triggered
put all of the background tasks you want to do in your app in Services which perform tasks in the background. In the service you should be able to define a timer that causes whatever updates you want to occur every x hours.
In the onCreate() of the widget, start the service. onCreate() is called every time that the widget comes to life (such as when the phone starts if it is on the home screen) and will therefore guarantee that the Service is always running.
Hope this was helpful.
Just for the sake of completness, I am linking here the official docs, where the issue of the rebooting is adressed.
http://developer.android.com/training/scheduling/alarms.html#boot

Categories

Resources