How to refresh activity while not using the app? - java

I'm working with realtime database where I want my activities to run even no one is using the app.
swipeRefresh.setOnRefreshListener {
// code
binding.swipeRefresh.isRefreshing = false
}
From the above code, the user needs to swipe to refresh, I want it to be the same but in background and it does itself.
That means the activity is like a 'refresh' in the background every five seconds.

It's not possible to keep an app running while it's not being used. Android app processes will be killed by Android when the user is no longer using the app, and this can't be prevented. Android does this to save resources and allow other apps to run when they are being used. I suggest reading the documentation to understand how it works.
Your app should isntead simply query the database again when it's launched and rebuild the UI.

Related

Android app can't run in background more than 5 or 15 minutes

I know that it is a real problem with running apps in background for different OEMs, but how can I solve this problem?
My app has a webview integrated from an online radio, everything is ok, but after 5 minutes (in locked screen mode), the player stop playing... I can configure my mobile settings, going to Apps, Special access, Optimize battery usage, search for my app and disable that button, but I win only 10 minutes (15 in total), and the app will stop again...
I found something about services, but I'm a really beginner, I don't understand why should I use a service to run in foreground... I have also a notification icon (+title+message) which is showing on display even if the mobile screen is locked. For my understandings, that means the app is running in foreground and in background. Can't figure out how to solve this.
I'm a beginner, but I want to go with this app in production (Google Play), and I want to be useful, not to be just another app...
I hope somebody will have the patience to respond on my issue. Thank you!
(At least some advice, what should I do...)
You need services to run in the background when your application is not visible to the user. Android automatically kills some apps especially when they are resource intensive. This can also be done by some antivirus software, task cleaners, memory cleaning apps etc.
You need to build your application around these challenges because users will not be required to optimize their settings for your application to run.
This services can be triggered by some android activity lifecycles. When you lock your screen, some life cycle methods like the onPause() and the onStop() could be called in your applications by default. You need to handle these events.
Services
A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. You need to create a service that will perform the tasks you want and periodically update the call back in your application.
E.g. The app may fetch notifications from a remote backend and periodically show them to the user at the notifications panel.
Android activity life cycles
As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.
References
Android Services -> Learn about android services
Activity Life Cycles -> Learn about activity life cycles

Store data locally on app close, not when put into background

I am working on an app that i want the user to be able to run it in the background, but i need to save some data whenever the app is completely closed. I have been looking at using SharedPreferences. I am having issues with onPause() and onStop() since those are called whenever the app is put in the background. I need to save data only when the app is completely closed and not when the user brings another app to the foreground and puts mine in the background.
That's not how Android works. You could be closed entirely hours from now. Or weeks. Or not at all and the user turns off the phone causing you to never save. The entire design of Android and its Activities is such that you should always be able to restore your state, and you shouldn't know or care that your app is being terminated. In fact there is no signal that an application is being terminated.
You could try doing it in onDestroy, but it isn't really a great idea. There's situations where onDestroy is not called.

Android Auto - Communication between background service and Activity

I'm attempting to add a MediaBrowserService for Android Auto to an existing media-player app. The app has a single activity which manages the MediaSession, Callbacks and related state. It has been set up so that it emits events, which the background MediaBrowserService consumes and uses to build its content tree.
This all works fine when the flow is like:
Start my app -> Start Android Auto -> Browse media
However, it falls flat on its face when the flow is:
Start Android Auto -> Browse media
...as in, when the app is not started and running in the background prior to when Android Auto is launched.
The problem appears to be that although my MediaBrowserService will be automatically launched by Android Auto, it does not create a corresponding instance of my app's Activity when it does so (which means no events to inform the MediaBrowserService, and consequently no content available in Android Auto).
Ideally it seems like the MediaBrowserService needs to be able to check and see if the app's "main" Activity is running, and spawn a new instance if/when it is not. But not sure if that's possible, and it tends to feel like it's the wrong approach to take here.
What's the correct way to work around this issue? I don't want to replicate all of the app's MediaSession handling and playback-related code in the MediaBrowserService implementation. That should be kept as lightweight as possible. Is there a way to ensure that the app's Activity is always running whenever the MediaBrowserService is active?
The answer to this was refactoring. Lots and lots of refactoring.
In a nutshell, Android expects Activities to be used to fill a particular architectural niche. Specifically, the niche where you're displaying an interface to the user via the device's screen. Other use-cases, such as having a 'headless' Activity running in the background, appear to be neither expected nor supported.
Thus the answer was to take all of the app's playback-related code out of the Activity, move it into a background Service, and provide an API for passing on the relevant commands (and receiving data, state updates and so on) either from/to the foreground Activity (if/when the user is interacting with the app's UI) or from/to the MediaBrowserService (if/when the user is interacting through Android Auto's UI). Starting the service is something that can be easily done from either context, if/when needed.
That appears to be the solution. It certainly can be tedious if you've got a nontrivial existing codebase you're working with. Far better to anticipate this sort of issue, and architect your app accordingly from the start; keep things that aren't directly pertinent to your app's UI outside of your Activity implementation(s).

Is there a way to put a running android app as the android device wallpaper?

In detail, I want to know if it's possible to take the output from a running Android app and pipe it to the to the background of the display. In other words, have the background wallpaper update itself dynamically.
No you can't do it this way. The reason is because Android starts your app with an activity (unless your app is just made up of services to run in the background for other apps to call). Whenever your activity starts, it comes to the foreground and receives user focus. The Android documentation states that if the user performs an action that starts another activity or switches to a different app, the system calls another set of lifecycle methods on your activity as it moves to the background. At this point, the activity is no longer visible.
The closest concept to what you are trying to do is a live wallpaper but that is a service and is a different paradigm.

Opening app from background Android

I have created an Android app that I am also running on BB10 devices. It has some functionality regarding call logs, but one of the most important functions is that the app has to open (assuming it's in the background currently) whenever a phone call has ended. Worded differently, it needs to foreground itself whenever a phone call has ended. I currently have it creating a dialog that opens within the app, but it will not appear until the app has been manually foregrounded. I do not have a requirement as to how it opens itself, but I do need a yes / no dialog to appear when it does.
Is there any way to have the app auto-open itself? Or is that not possible?

Categories