PARTIAL_WAKE_LOCK kills my mediaplayer - android

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

Related

Injecting screen_on event to make sensors work when screen is off

I am trying to make sensors work when screen is off. It is well known bug. And there is no any soution for this on all phones. I have the hypothesis that i can can cheat kernel if I send(inject) screen_on event while actually the screen will be turned off. Do you have any ideas how can i check my hypothesis and inject such event?
Why don't you start a background service that monitors the sensors?
And then send/use the results back to your application's activities.
Your activity will still pause when screen goes off, but you will continue to read the sensor data.
Also a good idea to do the heavy processing the background service as well...
This works great for me.

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.

How to release a wake lock?

I am setting a wake lock using wl.acquire(). This works fine. I need that wake lock as long as my application lives, so calling release() can only be done when the application is left.
Do I have to call release() somewhere? For example in onStop()? I would say no, but I am not sure.
if you refer yourself to these 2 pages:
http://developer.android.com/reference/android/os/PowerManager.html
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#release()
You should release the wake lock as soon as you can: therefore if your app is closing/pausing release it!
Also, word of advice, make absolutely sure you need a wake lock, when you need it and for ho long.
in my case I put one only for the in game screen to avoid the users screen going black while thinking (since it is an opengl app it takes a few seconds to fully load), but in the other views I release it.
Trust me when I say that it is annoying for a user to end up with a empty battery because an app was forcing itself to stay awake.
Beside the others useful answers you already received, I just found this interesting answer about how to force screen on, maybe you'll find it useful too, if you don't really need a wake lock.
as long as my application lives
how many activities your application has? you can release it in onDestroy() of last activity that gets popped by activitymanager.
And yes, you must release the lock especially if you are going to keep the screen brightness on, to avoid battery drain.

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.

Android Air - How to keep application alive during sleep

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

Categories

Resources