I am a beginner in Android development. I want to build a service that once started, must keep running.
Step 1.) Once user will install the app give some input, service will start. (i'll use IntentService)
Step 2.) Now user moves to other app/close my app. I want to make sure Service should keep running and processing/saving info with current timestamps.
Questions:
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
3.) If my app closes, related services will also close then?
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
Once user installs app, you can start STICKY service but make sure you inform the user as well as Android OS by making it run on foreground with on going notification. it will help your service to run for long time without being killed by Android OS.
1 month is long period, user may reboot the cell in between. and you will need to restart your service. so you should register for reboot complete event .
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
There is no condition like considered to be closed.Actually, when user installs app, it is by default in stopped mode, it gets activated when user opens your app for first time. and application can go back to stopped mode again whenever user force stop your app.
3.) If my app closes, related services will also close then?
If you are considering that app is considered as closed when user don't use the app,your service won't close unless Android OS kills it in low memory situation.you may make it foreground as suggested earlier to convey Android OS that you are doing important things,nothing is wrong,so kill me only at last when you don't have any other options left
Related
I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.
Is there a way to make sure that my Service does not get killed?
Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?
Foreground Service is the only way if you want to assure that the service will not be killed.
The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.
All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.
Basically you need to keep service running in the background,
Here is the workaround to achieve this
https://stackoverflow.com/a/58162451/7579041
above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc
I hope this will help you out
When I remove the app from the multitask list, it doesn't seem to kill the app's processes + services, but when I reopen the app, it starts from the "beginning" such as the homepage. What exactly does removing the app from the multitask list do exactly?
Generally removing an app from your multitask list is the same as quitting an active app (by hitting the back button continuously). The app stops functioning and the background processes linked to the app are killed. But, as you mentioned, it doesn't always happen because there are certain apps that require certain processes to run- for instance, closing an email app still lets it check for email and perform certain functions.
Force stopping your app is what actually kills all background processes as well, however again there are certain apps that still avoid this- this could be because they have some sort of link to the maker of the OS that allows those processes to run. For example, the Gmail app and Android OS both belong to Google, so they probably can decide how their app runs regardless of what you do as an end user.
I have android app using google maps API. When the app is running and user doesn't interract with it app still works and system doesn't go to sleep. However I need to close it if user doesn't interract with app some while. Is there any solutions?
There are a lot of posts regarding this topic already. A key point is that the OS handles this for you and it isn't necessary to force this behavior. If you really want to force it you could have a scheduler in your application triggered by the onStop() method. When the scheduled task executes you can then call one of the functions mentioned in the links provided below to kill the app.
How to quit android application programmatically
A timer that will kill android app after idle for certain time?
I have the following scheme in my application.
Broadcast receiver listen for an BOOT_COMPLETED action and sets repeating alarm.
Alarm starts a service via PendingIntent.
Service checks for some data by internet, and when new data are available, shows notification, which is runs an activity, when user select it.
All is working perfectly except one thing. When i close the app from Task Manager on my device, process is killed and my alarm no more works. So the process is stopped until next restart of device.
Setting android:process to different for the service and activity does not help. Debugger shows me that we have two different processes, but closing an app from the task manager kills both processes.
I have created two different applications, one just an activity, and the second one for broadcast receiver and service.
In that case all is working as i need. But now i have another issue. Two .apk files. I have tried to find a solution to merge two apk's in one for Market, but looks like its impossible. Ask user for installing two apk for one does not good idea i think.
So my question is how i can solve this?
When i close the app from Task Manager on my device, process is killed and my alarm no more works. So the process is stopped until next restart of device.
This is a good thing. The user, by killing or force-stopping your application, is saying "I do not want you to run again". Developers should respect their users wishes.
So my question is how i can solve this?
Treat your users with respect and allow them to force-stop your app if they choose. You can re-enable your alarms on the next manual run of your application.
You can also detect if they did this, by having your AlarmManager-triggered code keep track of when it runs -- if, the next time the user manually launches your activity, you determine that the alarm code has not run in far too long, that indicates the user force-stopped you. You might use this information to suggest that the user go to your PreferenceActivity and change how your alarms behave (e.g., run every 24 hours instead of every 10 minutes), so that the user does not feel the need to force-stop your application.
I created an new Android app and succeeded in its working. Its all functionality are working fine. While starting it will ask use name and password.
What my problem was "If my Application is idle for some 4 to 5 hours, then automatically it get quit , while restarting its again asking to login"
I need to know how to avoid automatic quit of my app.
I'm sorry if its simple or already asked quetions.
I need to know how to avoid automatic quit of my app.
No, you do not. Simply redirect the user to log in again, or, as #Rasel suggests, persistently cache credentials in a file or database or something.
Android applications do not and must not live forever. Phones have limited RAM. Android will terminate unused applications after a period of inactivity, to free up RAM for other applications. This is perfectly normal, just as it is perfectly normal for a user to close a Web browser after visiting a Web app.
Its completely natural for the android application.Android OS automatically kill the process when it needs to do.So if you want keep your application alive you have to think differently.To keep always running you can use service that will monitor your application states and handle the situation when it prompts for the login info again.
Another option you can write the login information in the shared preference and can clean when user intentionally leave the application.So when starting again if you find the information you can directly prompt to the user without entering the login information