Does disconnecting Android Phone Display Hardware Stop Apps - android

I am running an accelerometer based android app that will run for a few months while phone is on and does nothing else. Some phone allow display not to go to sleep at all which allows my app run fine infinitely. The screen also has only a black display and nothing else apart from background accelerometer listener and occasional http posts. My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?

My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?
In short, it depends on your app architecture (otherwise i.e. music players would require to keep screen on to work). Depending on task you are really doing you may use Alarm Manager to periodically fire your code, or use Service.

Related

Detect user interactions on device when the application is in the background

When Android device boot up, my application will start working in the foreground. User can put my application to background and he\she can use other applications for a desired long time.
When user stop interaction with phone for 30 seconds or lock the phone, my application will (if required)unlock the phone and continue to become active in the foreground.
How can achieve this?
User can put my application to background and he\she can use other
applications for a desired long time.
Starting from Android O you won't be able to reliably make your app work in background. System applies several restrictions on background processing especially running background services. Only alternative would be to create Foreground Service. But if OS detects that you are performing CPU intensive work this won't work either.
When user stop interaction with phone for 30 seconds or lock the
phone, my application will (if required)unlock the phone and continue
to become active in the foreground.
Unfortunately its not possible. Even if manage to get Administrator rights there is no API which allows developers to unlock the phone without user's action. This would be a privacy breach.

Prevent my app to sleep when device goes to sleep mode

I have written an app that should be running continually event if device goes to sleep mode.I have tried many things like services (with startForground also) , alarm manager and persistant="true", but when device go to sleep (by user or by device) , my app is terminated.and even PowerManager does not work too.
There is an option in device setting that is 'Keep running after screen off' (in huawei device : setting > app > my app options > battery > Keep running after screen off). So when this option is active , the app is running for ever.I want implement something like this in my app programitically.
I saw some apps that implemented this , but I dont know how.
So how can I do that?
Use ForgroundService.
If you check your device applications with an app manager, you can see that some of them using a sevice that is always running. even if you stop them, some of them start working again.
see more about services behavior in android developer.
and search for "keeping a service alive".
On some devices (Huawei, Xiaomi, LG) there is a special setting that contains a list of apps that are allowed to run in the background. It sounds like you are talking about trying to accomplish the same thing. This cannot be done on those devices. On those devices, unless your app is added to the list of apps that are allowed to run in the background ("Protected Apps" on Huawei), the Android framework will not restart your app if it is shutdown. There is no way for you to programatically add your app to this list, and there is no way for you to work around this feature.
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

MobileFirst app running while device is locked?

On the initial load of our application, we make hundreds of worklight invokeProcedure calls, that take up to 20 minutes to an hour to fully complete. (Each one takes less than 10 seconds, so works fine.) However if the device lock screen comes up it pauses the application and if I don't respond quickly enough to the device lock screen, the worklight invokeProcedure gets interrupted and stops our initial load process.
Is there a way to configure the application on Windows, iOS, and Android to continue when the lock screen is showing?
You may want to look at a combination of preventing the screen lock from occurring and the background handling Idan Adar suggests, to provide a more graceful and controllable UX.
For iOS, setting the idelTimerDisabled property at the right places in your processing could prevent the screen lock, and then if other external device operations occur, you could gracefully complete and save process state of the rest of your procedure calls, resuming them when the app becomes active again.
[UIApplication sharedApplication].idleTimerDisabled = YES;
A similar approach should be supported in other platforms.
This is not a built-in functionality AFAIK.
For iOS, you need to enable "background fetch" mode:
https://www.ibm.com/developerworks/community/blogs/worklight/entry/ios_background_fetch?lang=en
For Android you may need to do this with a Cordova plug-in: IBM Worklight - How to use Worklight in a background process
As for Windows... no idea...

Prevent that the app get stopped or paused by the OS

I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?
You cant do that from the activity, you'll need to create background service.
http://developer.android.com/training/run-background-service/create-service.html
Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".
This is still not 100% guaranteed:
The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again
The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent
However, it is the best that you are going to get, short of rolling your own Android OS version.
The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.
Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.
Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.
In Android 9 and newer you can prevent your App falling asleep with a battery setting.
Long click on your App -> App info -> battery -> optimize battery consumption
Here add your App from the list.Hint: maybe the menu entries have a different name, depending on your phone.

Keep android application open

On the Android Market there is an app called Sleep Timer, and it is a type of alarm clock that brings runs the alarm even though you locked your phone while on facebook. I made a type of app that detects movement however it only works if the phone is left on that app... How can I make it work when the app wasn't left upfront, but is still running in the background?
You should take a look at Android Services, which provide the functionality you seek. Basically they enable you to create components that run in the background even when the user switch away from your application.
You can find a very good introduction to them here: http://developer.android.com/guide/components/services.html

Categories

Resources