Android Air - How to keep application alive during sleep - android

If it is possible to keep an Air for Android application alive during sleep, how can it be done? I know how to prevent sleep and lock but this course is not preferable to some users.
Update
The reason in this case is to keep the video streaming from the phone to a server.

I think what you are talking about is preventing Screen Dimming for AIR apps. That is possible using the following:
NativeApplication.nativeapplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE
This overrides the default behaviour and screen never times out until the AIR app is in foreground. To take the mode back to normal you can set the property back to SystemIdleMode.NORMAL. In addition to this code, you will have to specify two permissions in your app.xml for AIR app, which are:
android.permission.WAKE_LOCK
android.permission.DISABLE_KEYGUARD
But as Emmanuel mentioned above, this would definitely mean that your battery will be consumed much faster. However, it could be useful in apps where Video is playing and you don't want the screen to dim.
Hope this helps.
Mohit

Why would you like to keep it alive? If the user no longer looks at his phone, why consume the CPU/RAM/battery?
If you want to do something in the background at regular intervals you could use an Alarm. http://developer.android.com/reference/android/app/AlarmManager.html
This lets you start the alarm even if the phone is sleeping.
If you want to do other kinds of background processing, you could use a Service running in the Foreground. http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29
This doesn't 100% guarantee that the service will always stay up and running though, e.g. when the phone runs low on memory.
I hope this is helpful...
Emmanuel

Related

How to use wakelock / make an app run when the device is locked

So I am trying to make an app that needs to use some networking stuff while running
in the background. (From a thread cause you can't otherwise and cause it's in the background)
But when I lock the device after a little while the thread just stops and the app is no longer running. I've been trying to figure out how to use wakelock to keep the device cpu running but I didn't manage to do that.
Even though I have the WAKE_LOCK permission in my manifest it tells me that:
java.lang.SecurityException: Neither user 10783 nor current process has android.permission.WAKE_LOCK.
Does anyone knows how to fix this or what is the proper way to do something like that?
Thanks in advance.
Well, to fix that problem you need the wakelock permission in your manifest. But its only going to be your first problem. Android is really meant to discourage background work when your app is not on screen, for battery optimization reasons. Just having a thread is not going to cut it- your app will be killed shortly after being backgrounded for resources.
If you need to do occassional background processing, look into WorkManager. It allows your app to be schedules for background work for brief periods.

Need for WAKE LOCK in my new Application if the same is already working through many popular apps working in phone

Do I need to implement WAKE LOCK in my new application if I feel the same is already implemented in many popular applications like GTALK, Whatsapp etc. and those would already working on my target mobile phones.
I believe WakeLock ( for example PARTIAL_WAKE_LOCK) prevent CPU to go into sleep. Means it makes awake device not application. If my application need to run in background and not to be off due to device sleep then WakeLock has to be implemented.
Correct. And other applications may also elect to use wakelocks. However, they will use wakelocks when they want to, which may not be when you want to. You only use a wakelock for a small piece of code where you need to keep the device awake, and you should not be assuming that anyone else happens to hold a wakelock when you need to run that small piece of code.

PARTIAL_WAKE_LOCK kills my mediaplayer

I implemented a wakelock to avoid stopping my MediaPlayer (playing local files only) when screen goes off. Since I don't want to drain battery, I tried a PARTIAL_WAKE_LOCK. But it has no effect: screen off kills my player. FULL_WAKE_LOCK works fine, but screen stays on, as expected, draining the battery...
What am I doing wrong?
Thanks!
L.
It looks like you are using the wake lock for the wrong purpose. From what I understood based on your explanation: you need the playback to continue when the screen goes off:
To do that - I would recommend you try the following logic.
Try to have the player running in a service (not in the activity)
Start the service with startForeground method (this will need you to include a notification as well)
Use a Messenger to communicate between your activity and service.
But the wake lock may come handy; as this may be helpful in handling a audio stutter issue in future. I am not very sure about this part as I myself is yet to try out the wake lock as a solution to stuttering issue.
(I am not a professional/commercial programmer ; rather an ad-hoc developer who finds a requirement designs a solution, develops it and uses it for myself. So my solution might not be a 100% professional approach but I am sure it does the job)
Good Luck and happy coding
- S.Mani
Wake Lock is to make sure the device does NOT go into standby.
What you want is to listen for Intent.ACTION_SCREEN_OFF that tells you that the screen is now off

Android: Foreground service vs. wakeLock

I am relatively new to Android, so what I am asking may seem obvious (although I have read all the similarly titled questions, and have searched extensively). I need to monitor the accelerometer continuously for long periods. Two approaches have been suggested:
1) acquire a partial wake lock that is held the entire time the acceleromtere is being monitored; and
2) monitor the accelerometer in a foreground service.
The first approach appears to use a lot of battery life. The second approach should result in a service that is only killed rarely, but I'm not sure what "rarely" means. Which approach should be used, and are there alternatives that I should consider?
Holding a WakeLock and a foreground Service are not really related and shouldn't be compared are to which direction is best.
Android OS is built to swap out processes based on a variety of factors. This means your process might get killed at any point by Android and it provides a framework to help you, the developer, to ensure your app can save and restore its state when this happens.
A WakeLock simply prevents the CPU from sleeping which helps save battery when the phone is not in use.
Now, a combination of both would help you achieve what you want but at great user cost. I wouldn't want an app in my phone to keep the CPU constantly running or a notification icon to show up constantly in the notification bar (that's what a foreground service does).
Keep in mind, starting a service in foreground mode does not guarantee your app will not get killed. It might still happen albeit rarely.
What is it you are trying to achieve here? Why keep monitoring the devices accelerometer? Perhaps you should only monitor it only when an Activity of your app is in the foreground instead.
I had exactly the same need and problem. I believe the solution is to use both a partial wake lock and a foreground service. Android will try not to kill a background service that holds a wake lock but is free to kill it when it needs the resources and possibly restart it later. That's fine for a lot of purposes but at least in my case that is not good enough. Putting a service into the foreground state is the way to tell Android that that killing it is unacceptable. Yes, it might still happen in extreme situations but that would now be a violation of the API contract whereas with a background service Android is free to kill it. You should therefore probably code as if that that will never happen but just know that this is a possible but probably rare error.

Android: Change delay before the phone is put to sleep while my app is running

This question sounds so easy I can't believe I can't find information on it (maybe I have the wrong key words in mind...)
I'm looking for a way to change the delay before the phone is put to sleep when my app is running.
I was using wake locks until now to prevent my app from being closed to frequently (its and opengl app and the loading time is a killer specially on slow phones).
I don't like the idea of indefinitely leaving the phone on (mainly because it empties the battery fast to have a full opengl app running).
Is there a way then to change the delay before the phones goes to sleep to 2~3min ?
I would also like to add that I do not wish to change the settings of the phone (that is only the users decision)
Jason
edited to correct the term "application going to sleep" which was pointed out as being incorrect.
Create a Timer object and release() your wake lock in the timer task. I'm relatively sure you can release a lock in a worker thread, but worst case, you'd need to use runOnUiThread if I'm incorrect.
Be sure to handle the case when onPause() is called and cancel your timer in that case and release the lock immediately.
In general, I'm not a fan of wake locks, but if this is to make your boss happy, wake-lock away.
EDIT: Btw, the system setting for the UI timeout is accessible by apps: SCREEN_OFF_TIMEOUT in android.provider.Settings.System. But really, this is for private applications only, public applications have no business changing this value.

Categories

Resources