I'm developping an app for Samsung devices with an activity that has to handle a NFC tag..
This activity is called when the app is launched and on resume.
I noticed that, when the phone stays awake with the NFC tag connected to it, after a while the device stops recognizing/scanning for NFC tags.
After trying with some other applications from Play Store, I noticed that this doesn't happen only with my app.
All I can do to make it work again is to switch phone's screen on/off or to switch nfc on/off.
I tried different ways to fix it, as to keep the cpu running, but none of these methods worked :
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();
and
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The only post about my problem that I found is : onTagDiscovered() not called any more when nfc tag already there after updating from 4.4.4 to 5.1.1 Samsung
Unfortunately, I'm working over non-rooted phones, and I'm not able to get a Samsung Knox licence (unless it is free ?)
Thanks in advance
EDIT
Tried with
setKeepScreenOn(true);
But still no change about this bug
Related
I've developed an application that streams music (via internet connection) using service and having trubles streaming content without phone going idle.
While i was developing my application each time i tried case mentioned below the music was reproducing fine.
Use case : search song, select song from results, play song, screen off -> auto play next song from result list
I'm developing using real device - Huawei Mate 20 Lite - OS v8.01 so while debugging it gotta use USB cabel.
Like i said following the use case above while hooked on USB the auto play while screen off works good. The case it doesn't work good is when the cable is not connected (only mobile data turned on).
What I've figured out is that phone when connected on USB is probably keeping the device awake and it doesn't go to idle mode while when not connected after around 5mins the device probably shuts down processes that cost energy or it shuts down connection to mobile data i'm not sure and there's where i need you guys.
Also I've tested app using HTC U Play - OS v6.0 and the streaming goes smooth without interrupts while screen off and phone wasn't touched for 10+mins.
Also I've tried to acquire wakelock inside oncreate and without releasing it just to see if it helps and it doesn't.
pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wl.acquire();
This problem you are facing can be due to the fact that after Andriod 6.0, all apps are getting optimized for optimizing the battery usage.
If you really think, the reason behind the application to go killed is inactivity. Then, probably, its because of battery optimization software itself.
You can enable another permission while installing the app on the device where you can update the list of unoptimized app by adding an entry for your app.
Originally, you will be able to do the manual settings by following below instruction.
. Head for the ‘Settings‘ app and then ‘Battery‘
. On the ‘three dots‘ menu, top right, you’ll find ‘Battery optimisation‘.
. Here you’ll see a list of all applications which shouldn’t be ‘optimised‘ (for which read ‘can be handled by Doze and App Standby’) – by default the list is usually very small, with almost all apps enabled for ‘optimisation’. Which is fine for general users, but if, like me, you want a few applications to live outside of the new battery optimisations, then tap on the ‘Not optimised‘ pick list and choose ‘All apps‘
. As you’d expect, every application on your phone is listed (this may be quite long) – swipe down until you find the application(s) that you particularly want to always keep running. Tap on the application name
. From the two choices, check the box for ‘Don’t optimise‘.
I have an app that uses BLE to exchange data with a BLE-capable device.
I used to develop and test it using Asus Zenfone Max 3 (Android 8.1) and I had no problems.
Then, I got an Asus Zenfone Max Pro M1 (Android 8.1). The app connects to the device but could not exchange any data.
After long investigation, it turns out that I had to go to Battery optimization in the smartphone's settings and change Bluetooth and Bluetooth MIDI Service to Not optimized and then my app worked fine.
I don't know whether this default setting is related to the OS (Android One) or to the phone model. But it is really a crappy thing because I am not supposed to tell every customer to do this (in his phone's settings) in order for the app to work !
So, my question is, is their a way to know, from the code, whether these services are optimized or not, and whether I can change these settings by code or any other ideas that could better fix this issue.
Add this permission to your manifest file:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
and test it like this :
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
reference
My question is a shot in the dark: what is the deal with Android 4.x / Jelly Bean? Are there known problems with services, stickiness, foreground, etc.?
Backstory: I tested my music player application successfully on all sorts of Android devices and emulators and then just recently on a physical Jelly Bean device (Samsung Rugby Pro). I found that the MediaPlayer onCompletion function is not being fired consistently when the screen is turned off. Most often it isn't fired for several minutes. When the screen is on, whether or not my activity is shown, the application works just fine.
(There are no problems on Gingerbread, KitKat, Lollipop, or Marshmallow. I have physical devices for those versions and they all work flawlessly.)
Device Information:
OS VERSION: 3.0.31-656355
RELEASE: 4.1.1
DEVICE: comancheatt
MODEL: SAMSUNG-SGH-I547
PRODUCT: comancheuc
BRAND: samsung
DISPLAY: JRO03L.I547UCBLL1
CPU_ABI: armeabi-v7a
CPU_ABI2: armeabi
HARDWARE: qcom
ID: JRO03L
MANUFACTURER: samsung
USER: se.infra
HOST: SEP-125
I figured this out; I hope this helps you too ...
The problem is not in firing the onCompletion event but rather that reset() blocks indefinitely. I had previously attributed this to Android issue #959: MediaPlayer "crash" (deadlocks the calling thread) when resetting or releasing an unused MediaPlayer which was incorrect for my case.
My particular problem was aggressive sleep behavior on my test device. This bug would pop up when I ran in Airplane Mode with few other apps because the call to reset() (or release()) also releases the MediaPlayer instance's WakeLock. Once that occurred the device would sleep and the code would appear to block in that function. I was unable to reproduce this with a debugger attached and/or plugged in because either of those prevented the device from sleeping!
My solution was to create a separately managed WakeLock which I acquire() basically when playback begins and release() after all songs have played through. Technically, I do this only when the activity has been paused and also release on resume because it's unnecessary to hold a WakeLock when the device is awake already.
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag");
wakeLock.acquire();
I'm having a problem with my app when I try to stream musics on background and it only occurs on some devices.
When the app plays any music on foreground it works without problems, but in some devices, when I press the power button, the stream immediately loses its quality (looks like when I'm on a low speed internet connection). When I turn on the screen the stream gets better again.
I've already tried WakeLocks but it didn't work.
Edit 1:
This is how I used the wake locks:
OnCreate of my activity:
//Setting the wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
and than I do this when the music starts:
wl.acquire();
and this when the stream stops:
if(wl.isHeld()){
wl.release();
}
Edit 2:
Tried this as well:
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
Edit 3:
Tried with WiFi Locks too:
//Setting the proper lockMode depending on the android version:
int wifiLockMode = WifiManager.WIFI_MODE_FULL;
int sdkVersion = Build.VERSION.SDK_INT;
//WIFI_MODE_FULL_HIGH_PERF was added on Android 3.1 so
//I need to implement this to make sure the wifi will execute on its full power(even if it consumes more battery)
if (sdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR1) {
wifiLockMode = WifiManager.WIFI_MODE_FULL_HIGH_PERF;
}
//Setting the WifiLock
WiFiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiLock mWiFiLock = wm.createWifiLock(wifiLockMode, "MyFlag");
mWiFiLock.acquire();
//Releasing the WifiLock
if(mWiFiLock.isHeld()){
mWiFiLock.release();
}
With the WifiLock it seems to be a little better(or I'm getting used to the interrupted sound)
Any ideas?
I had the same quality issues with Nexus 4 using android 4.3 and discovered that the problem was solved if I turned off the "wifi optimization": Wi-Fi-> Advanced -> Wi-Fi optimization.
Seems consistent with this other threads:
https://code.google.com/p/android/issues/detail?id=42272#c319
http://forum.xda-developers.com/showthread.php?t=2072930&page=55
If this is true, wifi lock is not preventing the wifi optimization. I also tested that the wake lock is not required to stream quality music with screen turned off, at least with Nexus 4.
I think that you cannot do something really because I think that each OEM rewrite it's "battery optimization" rules that will try to preserve battery when the screen is OFF.
The best thing to do is to follow Android Standard about MediaPlayer's wakelock system and cross your finger :)
I have an application in which I am sending network data over WiFI. Everything is fine until I turn the display off or the device goes to 'sleep'. I'm already locking the WiFi however, it seems to be the case that the CPU speed ramps down when in sleep which causes my streaming to not behave properly (i.e. packets don't flow as fast as I would like as they do when the device is not sleeping).
I know that I possibly can/possibly should address this at the protocol level however, that might possibly not be possible as well...
Is there any way to "prevent the CPU from going to 'sleep' when the screen is off"? If so, how? If not, any advice on how to keep the speed of my WiFi stream consistent whether the device is in sleep mode or not?
Grab a PARTIAL_WAKE_LOCK from the PowerManager. You'll also need to add the WAKE_LOCK permission to your manifest.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();
//do what you need to do
wl.release();
Okay, so, after much more research and experimenting, it seems that the real issue is the fact that, at least on some phones, their WiFi goes into a 'partial sleep' mode EVEN IF you've taken the WiFi lock. It seems that this is what the 'WIFI_MODE_FULL_HIGH_PERF' flag was invented for when taking the WiFi lock... unfortunately, this flag is only available on some devices/Android versions (I have no clue as to which but, it wasn't available to me). So, therefore, it isn't a fix for all devices.
The only "solution" (which is actually a kludge) seems to be to 'detect when the screen is turned off and then, set an alarm that turns the screen back on immediately thereafter'. The links that helped a little bit with this are:
How to keep a task alive after phone sleeps?
and
http://android.modaco.com/topic/330272-screen-off-wifi-off/
I hope that this helps people who are experiencing WiFi disruption when the phone goes to sleep/screen is turned off (and the phone is unplugged/disconnected [e.g. you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]).