Keep Android app running in the background - android

I'm working on an app that streams live audio to the device. But, with some devices when the user is browsing the internet the application stops the audio playing. What's the best way to fix this problem?

You can use an Android Component Called Services for this kind of background activity. A Service is an application component that can perform long-running operations in the background and does not provide a user interface

Related

Keeping a video call app running when switching apps in Android

I am developing a video call app, and all of my camera, networking, encoding, decoding and audio are running in my activity.
The main problem is that whenever the user locks their screen or switches apps, the activity is shut down and I lose the call.
I've tried wakelocks, notifications, foreground services, nothing keeps my activity alive past Android 10.
Any suggestions?
It is normal for the activity to disappear when the memory is insufficient. You need to use the foreground service to keep the program. Just like the music player, they all use the foreground service.

Run some code when app is killed

I'm trying to develop a mobile application which interact with ibeacons !
I'm developing this application for iPhone and android ! But i have problem !When i was developing iphone app, everything works,
i receive my notification even if my app is killed !
But on android i don't know how can i develop that !
If my application is on background, it works but if i kill my app, nothing happen !
Do you have an idea to run my code even if app is killed?
Thank by advance!
If you want run some code in background even if your application is killed you should use services. From google documentation:
A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
If your app runs in scheduled times you can use JobScheduler or AlarmManger. JobScheduler and AlarmManager wrok with services.
You can use Service. It will still working even if your activity is killed.
https://developer.android.com/guide/components/services.html

How to access internet my app app is minimized on Android Mi4i phone?

OS moving network state to BLOCKED, Whenever minimized the app.
Is there any steps to request for allow to access the internet, without move app to foreground.
Since the app is minimized and you want to perform network operations, go for a background service. It would keep running in the background and perform the network operations for you.
See this page for the steps to create a background service.
Android Official: Create background service

Can anyone confirm my example of a background vs foreground process?

I know that a process is an executing instance of an a program running in the foreground or background and that background processes run asynchronously(runs outside the main thread).
Would background music in your application be an example of a background process?(doesn't freeze up your UI in the main thread and it runs in its own thread)
Does process imply then that another program is running the music in the background?
I don't think that you need another process for playing music - you just need another thread. I don't think that you want to play music while your app is not in the foreground. For example if your app is a game which only produces sounds when it is active.
Obviously this is not true if your app is a media player which still plays music while in the background letting user interact with it using notifications which let the user play/pause, skip a song or stop playback.
Please see a question like this one: How to put media controller button on notification bar?
Your android application consist of process, services, threads, message queues. It application developer choice when to use what. As good developer, you should always try to make you application user experience smooth without and any hang. Always perform heavy/time consuming activity with service or async threads, and avoid such activity on main thread as it cause UI hangs.

What's the reason or benefits that I must start a Service to control background mediaplayer?

I read Anndroid document - http://developer.android.com/guide/topics/media/mediaplayer.html
which said ".. you want it to continue playing while the user is interacting with other applications—then you must start a Service and control the MediaPlayer instance from there. "
But I found if I start a local a music file from an activity, then leave that activity, ( for example, press HOME key and interact with another app ), the music continues playing.
So, I don't understand " the "must start a Service" in document. Did I miss something?
This was not a big obstacle for my app at this moment. I am just wondering what's the potential problems could be if I do not use Service.( Services have longer lifespan, so the mediaplayer could be killed earlier, any others ? )
Our development is based on Android 2.2.
Thanks for any help in advance.
Big reason, if you are not using a service, users cannot listen to music outside of your app when the the activity gets paused or terminated. Technically you can make a music app but if your users cant listen to music when another app is in the foreground or the phone is in a different state(locked) it wont make for a very good app. You should take a look at the activity lifecycle for a deeper understanding of the process. Note that this behavior is by design for saving power, memory and cpu cycles.
It helps also not to think of services in the more traditional desktop dev usage. You want this thing to live even when your activity is not up and about.
For more about activity life cycles Managing the Activity Lifecycle
For the How
http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/
For the why
Why is it important to use Services for background tasks?
Playing the music in the Activity might be fine for now, but when Android is low on resources it might try to kill your Activity. When you add a service to an app, Android will try to keep that process alive as long as possible if it falls under certain criteria (such as playing music). Read over the Process Lifecycle section on Services:
http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
The activity can get killed by Android or by the user and then the music would stop playing.

Categories

Resources