Right now my app has to stream music. I'm using a web server and everything works if the phone is awake or is plugged in to a power source.
But I really need to continue streaming until the phone go to sleep. I'm using PowerManager PowerManager.WakeLock in order to keep the phone awake.
This is part of my code:
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.setReferenceCounted(true);
After that I have the wl.acquire(); and the release. I have a handler that does the release 5 minutes after the songs stop streaming.
int timeToWait = 300000;
turnOffDevice.sendEmptyMessageDelayed(0, timeToWait);
But it works for a short period of time then the song starts jumping and most of time the songs stop.
Hi I've already fix my problem, the problem was in the wifi, it was sleeping after some time so I added a wifilock and in the constructor of the class in I'm using WIFI_MODE_FULL_HIGH_PERF this flag avoid the device turn off the wifi
wifilock = manager.createWifiLock(3,TAG);// WIFI_MODE_FULL_HIGH_PERF
The only problem is this flag is only available since API 12, but for now is not a big deal into my project. If you want more information in this linkWIFI_MODE_FULL_HIGH_PERF
Related
I started songs on one of the music apps, on my android device.
Although I can see only partial wake_locks being acquired, still device display doesn't turn off even after screen timeout.
I wonder what is keeping the device screen on.
Can someone suggest where to look for probable cause.
Thanks!
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
// This will make the screen and power stay on
// This will release the wakelook after 1000 ms
wakeLock.acquire(1000);
// Alternative you can request and / or release the wakelook via:
// wakeLock.acquire(); wakeLock.release();
I used this code and works fine to me.
I'm working on a service-like application using android's AlarmManager. The app basically connects to a server and calls a remote procedure. If the value returned is True, it plays a sound:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
try {
mediaPlayer.setDataSource(getApplicationContext(),
Uri.parse("file:///system/media/audio/ringtones/Rolling_tone.ogg"));
}
...
mediaPlayer.start();
This works all and well when the application is started manually. However when the phone is rebooted (I've implemented the BroadcastReceiver), the sound is played for like a second then gets interrupted immediately.
It sounds almost like the play cycle is being cut off by something. I need to stop and re-start the application to have it working correctly again.
Any clues on what the cause could be?
You could use MediaPlayer's setScreenOnWhilePlaying() to ensure that your application doesn't go to sleep while the audio plays in the background.
http://developer.android.com/reference/android/media/MediaPlayer.html#setWakeMode%28android.content.Context,%20int%29
Another possible solution is the use the PowerManager to keep your device awake with a WakeLock:
See http://developer.android.com/reference/android/os/PowerManager.html
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
... wait until sound has finished playing (with listener)
wl.release();
I am developing an application which should detect user movement and when he stops for more than 5 minutes it should sound an alarm. I was able to detect movement with accelerometer but the problem is it doesnt work when the screen is off. I have also tried using partial wakeLock. Re-registering accelerometer doesnt work either (this should be workaround for motorola devices).
Maybe I can do this using GPS and sound an alarm when GPS speed is less than 1m/s for more than 5 minutes but I am not sure if I will receive GPS updates when screen is off.
So I need a solution that will detect user movement even is screen is off on most devices. Any ideas on how to acomplish this?
Thanks in forward
You should acquire a partial wake lock for this kind of operation. Use the PowerManager class.
Something like this:
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();
You need also this permission in the AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Is recommendable using lock.release(); when you're done your work.
EDIT:
Also, this article could be useful for you.
partial wake lock this is what you need to access while your screen is off.
private PowerManager.WakeLock mWakeLock;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();
And after you're done, just release the lock:
mWakeLock.release();
If you obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().
many things has been written about wakelocks in Android. I'm using wakelock this way:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial");
partialWakeLock.acquire();
// few hours work
partialWakeLock.release?
In most of devices, PARTIAL_WAKE_LOCK can keep device running whole night. But there are few models (Samsung Galaxy ACE 2 (i8190) - 4.1.2 etc.), which ignore this kind of wakelock. Wakelock is held, but phone goes to sleep if device's screen is turned off.
Is there some way how to determine, if application is running in device with this problem? I can let user turn devices's screen off for a few seconds and check if work has been done, but is there some automatic way?
Thanks in advance.
I want to keep Wi-Fi enabled when device goes to sleep mode, I tried several options, but nothing has worked out like acquire Wi-Fi lock and set wi-fi enabled. These options are not working then I tried with wake lock, this is working perfectly when my application running in the foreground, but when application is running in background, after some time excess wake lock error comes and application is getting destroyed and on top of that I can't use wake lock all the time because it dried out the battery.actual requirement is my application should run 24/7 and connectivity with the server always stays on because server can send data any time, but when device goes to sleep mode wi-fi is getting turned off so I need to set the wi-fi sleep policy to never upon starting of my application and will set back to normal policy when application gets destroyed. I tried following code in my main activity and runs the application and allow device to go to sleep mode and after some time connection is still getting closed:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
So please do help me to resolve this issue.
Use import static android.provider.Settings.System.WIFI_SLEEP_POLICY; instead of the name string Settings.System.WIFI_SLEEP_POLICY.
It works perfectly.
In my case Settings.System.WIFI_SLEEP_POLICY does not work. I was able to keep wi-fi on with the PowerManager
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Then in the OnDestroy() I call
wl.release();