Running app while the device is off - android

I have a very simple question. Can you force an app (from a development point of view) to run while the phone is off and plugged in. I know with my phone there is a battery display that shows while the phone is off and charging which I assume is controlled by software but I was wondering if that is possible with an app. After research my gut is telling me no but I want to make sure.

I know with my phone there is a battery display that shows while the phone is off and charging which I assume is controlled by software but I was wondering if that is possible with an app.
This is not possible from an app, only from firmware.
If you are not looking to update the screen, you can use a WakeLock to keep the CPU on. Or, use a different WakeLock and keep the screen on all of the time. Neither of these will make the user very happy, unless it is done completely under their control.

You will need create Service and on create or when you are about to do whatever your app does, create and acquire WakeLock. Once it's done you can startForeground to let user know that you are doing some important job and let Android to keep you alive/process and let your app finish work.

Related

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.

Bending the Android platform rules for parental app to a max

because my questions might sound a bit suspicious, first some background: I'm writing an parental screen time control app. I am a seasoned java programmer with some Windows Phone background just learning the Android platform.
The app would run at startup (I understand that services can listen for a boot event and run) and when a violation of screen time is detected it would nuke all user processes that weren't around when the service started. Or lock the screen or some other annoyance every minute. No problems, right?
The kid will the try to terminate the process. I understand that this cannot be prevented on an unrooted device but a pair of processed could be used to bootstrap each other when the sibling dies?
Then the kid will try to uninstall the application, something that is also not possible on unrooted devices. I can listen to the uninstall event but by then the app is gone. Here is the tricky part, can I somehow reinstall automatically here? The application can ask any permission it wants on first install if it can help with the background reinstall
I understand that many of the features required here are nontrivial because that could be used for making, well, PITA apps. But I'm just trying to redistribute the misery my kids are giving me with screen control ;-)
Thankful for pointers,
A sadistic parent
you are looking for an Admin app.
this will allow you to put some restriction. and you can listen for event for deactivation where intern you can reset the device if necessary.

capture hard-button events when phone is locked?

i am currently collecting information, if it is possible to make some kind of quickdraw-application for emergency purposes; i want to get the device to start a customized camera-intent once some certain key-combo is pressed.
I have check quite some information on the topic already, but it is still not clear to me how doable this whole idea is.
So far i figured, that it is not meant to be by android-design. My first idea was to build some kind of InputService and make it teach the hard power-button to listen for a triple-click. But the Services arent allowed to catch KeyEvents. So, the volume-button is out of the equasion aswell.
Then i came across the idea reacting on the SCREEN ON/OFF event, but 80% of the ppl say that it is not going to work, while 20% state, that they got it. But im not really sure if they really did it.
Last but not least the question, if it is possible to launch this customized-camera-intent/application out of the locked mode.
resumé: i need to check if its possible in general to launch this camera-app (that does some quick fotos for emergency purposes) while phone is in the pocket in lock-mode. Anybody got some opinion for me? thx
The solution is to setup a BroadcastReceiver that is listening for the SCREEN_ON-Event. I did this one inside a backgroundservice that was waiting for at least two SCREEN_ON-Events within a short period of time. (less than two events obviously can't work)...
The Service then dismissed the Keyguard and was able to even pass past password/gesture and run the App. Funny fact: closing the app does pop back to desktop still beeing logged in.
I did this at Android 4.2.. Sources told me that dismissing the keyguard is not working with current Android-versions no more.

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.

How to restrict users to shut down apps on Android?

I have a request on developing enterprice tracking application. The application should be able to get current postion of the phone and send it to server every 5 minutes. Even if application is running on background - user can easily shut it down. I need to create some sort of restriction for that, maybe password based. In order to shut down this application user must enter a password. And if he is rebooting, application should autorun after rebooting.
Is it possible to do this?
No it is not!
But... I know a trick that can do the work, it's name is AlarmManager
You can set alarm for your application that will alert every 1 sec, and if the application been shut down it will rebuild it self.
At this point you can set a password to shut down the alarmManager.
But just to let you know, I been trying this once, Google told me nothing, but users left me a comment that the application is hacking they device ;)
From ICS onwards, any app implementing Device Administration cannot be stopped. But the only drawback is the user can remove the Devvice Administrator.
You could restrict your app to work only when Administrator is On.
One more way is if the user removes the administrator you can lock the whole device with a custom password using resetpassword.
If the devices were rooted you could implement that but you probbaly wouldn't be able to distribute the app as an APK due to the required modification of system files. Maybe as a Zip that is flashed via Clockwork mod. Beware of voiding the devices' warranty though.
Babibu's suggestion regarding Alarms is a novel approach, but be cautious of waking the system constantly as that will drain the battery much faster.
Actually a big problem you'll face is that the user can disable GPS and then you can't programatically turn it back on again - unless you try to use an exploit which is obviously not ideal and won't work on all devices.
A better idea might simply be to report to your server whenever the user disables the application or GPS. You'll know which user disabled it so your organisation can punish that user appropriately.
What you are looking for is Device Administration
http://developer.android.com/guide/topics/admin/device-admin.html
This page explains how it works and how you can change all sort of policies about passwords and disable camera.. et cetera
What they barely talk about is as soon as an application is enabled as device administrator you can't kill it or uninstall it without disabling this feature first. It's up to you to implement enable/disable buttons the proper way.
To be sure the service always restart even if killed by system (in low ressources cases) you need to override this method of your Service :
public int onStartCommand (Intent intent, int flags, int startId)
{
return START_STICKY;
}
Finally to make your service start at boot you need to catch ACTION_BOOT_COMPLETED broadcast.
The main issue is you can't prevent user from disabling GPS or using mock locations but you can detect it and log it.
It is a good practice (actually it is mandatory in my country) to notice users they are watched so you can explain them at the same time that you'll know if they mess up with the GPS.
Rooting the phone is another solution but you will likely open more doors than you close ;)

Categories

Resources