Prevent my app to sleep when device goes to sleep mode - android

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);

Related

I need to prevent users from altering settings of my app

I want to prevent users altering settings of my app by throwing up a splash screen to block interactions whenever they go into the device settings and open the settings page for my app.
Here's a video of another app that does this: https://inversatechnosoft.com/wp-content/uploads/2021/03/WhatsApp-Video-2021-03-05-at-7.40.24-AM.mp4
This isn't possible (at least for apps that aren't part of the system image), for good reason: that's the sort of thing malware does to take over a system.
Even if you could do that, that isn't sufficient: an app could also be uninstalled from the launcher, or other apps. You could make it harder through various tricks, but ultimately the user could simply reboot the device into safe mode and do it there.
The only cases where you could have this level of control are managed devices or within a work profile.
I don't think there is an incredibly clean way to do this, but you could start a service which regularly checks the foreground task.
You can then use the ActivityManager to get a list of all running app processes with ActivityManager.getRunningAppProcesses() and then check their importance against IMPORTANCE_FOREGROUND.

Run a background service in android (API Level >=25) by disabling battery optimisation

I have seen many apps running in background without putting a notification in front of user, like some call record apps etc, I am developing an app which runs in background with getting killed by android system, In those apps i have seen in settings that one option called "App can run in the background when not in use" is checked. So now how can i implement this programmatically without showing annoying notification to the end user ?
Note: In android Oreo the background service runs flawlessly when the battery optimization is turned off for that app manually in the settings.
You can see the settings menu image in the link.
enter image description here
Thank you for in Advance :)

Does disconnecting Android Phone Display Hardware Stop Apps

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.

Android Lollipop - prevent task manager from ending my app

My goal is to make my app unkillable.
Application has admin permissions granted.
I have one activity and one service.
Applionation cannot be uninstalled while Admin permission is active (thats good).
Service is auto re-creating. That's also correct.
Unfortunately on Android 5 - Lollipop user can click on all apps button - then go to Task Manager and simply END my app without any problems. After that service is destroyed for good (activity too obviously...)
Is this a way to prevent that?
1) Xposed. At the cost of execution speed and dependency on the TaskManager that you will have to reverse-engineer a bit...
2) USER_PRESENT and friends. Your application can register a BroadcastReceiver and re-start on events. That is, it will be killable, but it will restart.
I think you are interested in something like a kiosk mode: displaying a single app and preventing to break out from it.
There are various tutorials out there. This one is quite good:
http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
Edit: if your main target are Android 5.0+ devices you can also make use of screen pinning. http://developer.android.com/about/versions/android-5.0.html#Enterprise

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