NanoHTTPD stopped when I play games - android

I have an android application that starts a server NanoHTTPD. The problem happens when I start the server, I leave the application in the background, and I start playing a game or a heavy application. After a few minutes the server is stopped. How I can keep the server started, even playing games or using other applications? (like AirDroid)
Thanks in advance.

It'll get killed by the Android system unless you place it into a Service and make it a foreground service.

Related

Android background Service vs foreground Service

I have built an android app that performs some downloads followed by some time consuming processing. My goal here is to do the downloading and the processing in the background. I have read about background and foreground services, but I am not able to understand them properly and which to use where.
I have built the rest of the app with ionic. Now I have to make the app work in background. I have tried cordova-plugin-background-mode available in ionic but unfortunately its not maintained anymore.
So what should I do to my app in android studio to make it support background processing.
Also is it possible to combine android packages to an ionic project after building it?
Thanks in advance.
First understand about Android services: Three different types of services:
1. Foreground service: is a service that stays alive even when the app
is terminated. Foreground services continue running even when the
user isn't interacting with the app.
List of Apps:
Music player app that plays music in a foreground service
Fitness app that records a user's run in a foreground service
Navigation app, allows users to get turn-by-turn directions
Even you perform your download
Note: To download and the process in the background Google recommend you to use WorkManager.
Let's understand background work:
An app is running in the background when both the following
conditions are satisfied:
None of the app's activities are currently visible to the user.
The app isn't running any foreground services that started while an
activity from the app was visible to the user.
2. Background service: is a service that runs only when the app is running so it’ll get terminated when the app is terminated. It performs an operation that isn't directly noticed by the user.
List of Apps:
Downlead data from server
Continuously share location
Sync data with server also use workmanager
IOT Apps
3. Bound service: is a service that runs only if the component it is bound to is still active. A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
All above apps can be bounded or not

How to proceed playing music after app have been killed?

After android appication have been killed, all the inner threads are stoped. How to proceed playing music using service and threads? Should I keep time of music in killing time and re-play from that exact time?
EDIT
I do use service and it plays in backgound. The only thing I want it to play when the app have been killed by user, in other words the music must be contolled only via notification buttons and not be related to application lifecycle
Make your service a Foreground Service. Then it will run even after the app is killed.

Run an android application continously for testing purposes

I am trying to run an android application continuously for testing purposes and noticed that the app stops after few hours. So I implemented an AlarmManager to start the activity every few hours, even with this I cannot run the app with out stopping.
I see the AlarmManger restarts the activity for few times and even it dies along with the App. Could someone suggest me how to achieve the functionality.
I really appreciate your help!
Thanks.
PS. I am testing the phone functionality using android like: cameraTest, ModemTest(playing audio, video files), Making Call, Bluetooth etc.
You could create a Service that restarts the app every x minutes or something. From the docs:
"A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. "
More here.
You can create a Service with a thread continously monitoring your application status(whether running or not) and it can launch a broadcast message to your application in case it is not running.

Will android services stop when I receive a phone call?

I have a doubt in Android services. I wrote a simple application with a service that runs continuously and upload data to server. My doubt is that will the service stop when I receive a phone call? Will it run in background when I make phone call or will it stop. Please give me an answer.
Thank you.
Activities stop, however services do not.
http://developer.android.com/guide/components/services.html
Services run on the background no matter what happen on the screen. It only stops if you stop it using stop or exit function of its main application or you stop the services in Settings.
Cheers,
The short answer is no. Unless the available phone memory was already low to begin with because you have a lot of apps running in the background, only then might Android forcibly close the service to accommodate the phone call.

Android retain AsyncTask state/progress

In the app I'm developing atm. I use asynctasks to upload videos to a website, as it stands now if the application process is killed (User returning to home screen using the back key), those asynctasks are lost. ideally I would want the uploads to carry on despite the application process being killed, but I don't think that is possible.
I wonder if there is a way to retain their progress somehow (Maybe support from the website API is necessary?), or if not at least save the details of the asynctask and restart it when the app is opened again.
Vimeos application seems to have been able to resume video uploads, even after having killed the application process, thats exactly what I'm hoping to achieve.
Appreciate any ideas and suggestions.
I think you may be using the wrong architecture.
Anything that needs to survive in between Activity transitions is more suited for a Service. A service runs in the background (possibly even after the app is closed) and lets you do long running things such as performing uploads.
To kill the app process but have the Service continue to run, you can assign the service to a separate Android process using android:process in the manifest.
http://developer.android.com/guide/topics/manifest/service-element.html#proc
See this thread too:
How to keep a service running in background even after user quits the app?

Categories

Resources