There are a few questions already on StackOverflow related to turning the screen on / off, however not a single solution has worked for me.
I do not want to use a wakelock as it's no longer recommended. My app wakes up the screen using WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, which is far simpler and doesn't lead to a situation where one can drain the entire battery if you forget to release it.
Setting brightness to 0 like the following code also does not work. On my two test phones (Samsung Galaxy S2 and S3), this code only dims the screen, but does not turn it off.
layoutParam.screenBrightness = 0;
layoutParam.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);
My scenario - it's a VoIP app which turns on the screen for an incoming call, but then needs to switch off the screen (but keep the CPU running) if the user holds the phone up to their face. The proximity sensor code is running fine.
Have you tried changing the Screen off timeout value?
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
The value of 1000 means 1000 milliseconds, or 1 second, you can change it if you want.
Afterwards change it back to the standard like this
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,-1);
This requiers a permission in the manifest file:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
Related
I have an issue with Android Vibrator. Basically, I have only one Singletone - scoped class incapsulating the android.os.Vibrator in a way, that it has two methods
startVibrating() {
if (VERSION.SDK_INT < VERSION_CODES.O) {
vibrator.vibrate(PATTERN, 0);
} else {
vibrator.vibrate(VibrationEffect.createWaveform(PATTERN, 0));
}
}
stopVibrating() {
vibrator.cancel();
}
The issue is that on some Samsung devices the Vibrator stops when the phone screen is turned off even I have a WakeLock for CPU.
Recently, we've got into the same situation.
According to Android docs:
If your process exits, any vibration you started will stop.
And you would expect that if you have any CPU-related WakeLock, it should work fine. However, we found that Samsung devices on Android 6 and Android 7 stop the Vibrator in some other cases too. For example, each time when the screen is turned off using PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, even if you take some other wake lock explicitly.
The good news that Samsung stops the Vibrator but it does not prohibit it from the future starts.
We've ended up with the next workaround:
When the screen goes off, the current activity gets onStop() call. So, we check if we should vibrate on that moment, and if we do, we simple start the Vibrator again. Maybe, not the best solution, but it works.
I want to write code that will disable battery charge when reaching 80% and enable when gets to 20%, should work on LG K4 5.0.
I have been looking around and tested a lot of solutions but none of them work.
Changing sys/devices/platform/battery/power_supply/ac/online to 0 or
sys/devices/platform/battery/power_supply/ac/present to 0,
cause android to crash and reboot.
I have found a file called LGBM_AtCmdChargingModeOff when changed to 1 it stops charging but can't change it back to 0, after reboot, it's 0 again.
Any idea on how to change this file back to 0 without a reboot?
cat doesn't work.
vi doesn't work.
paste with value = 1 doesn't work.
Just use Chargie (google for "chargie stick"). It's a Bluetooth charge limiter that does exactly what you want (hysteretic charging).
Android 5.0 includes a new way to control which apps are allowed to make noise on your device: when you press the volume button, the popup now lets you to choose None (completely silent), Priority (only priority notifications make sound), or All (everything is allowed to make noise.)
I would like my app to be able to query the device to find out which of these three modes is currently active, and also I would like to be able to change these modes (without requiring the device to be rooted). Does anyone know how to do this?
So far, all I can find is a brief reference on this changelog:
Setting the device to RINGER_MODE_SILENT causes the device to enter the new priority mode. The device leaves priority mode if you set it to RINGER_MODE_NORMAL or RINGER_MODE_VIBRATE.
This works as described, which allows me a very limited ability to change "priority mode" by modifying the ringer mode in AudioManager. That's not enough, though, as I need to be able to know exactly which of the three priority mode settings is currently active, and it would also be nice if I could change them more precisely than AudioManager allows.
I've found a solution, but this requires root to change, because this setting is in Settings.Global.
Name of setting is "zen_mode".
Values are:
ZENMODE_ALL = 0;
ZENMODE_PRIORITY = 1;
ZENMODE_NONE = 2;
EDIT: I've found another solution. Check NotificationListenerService.requestInterruptionFilter(int interruptionFilter). https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
Implementation example: https://github.com/kpbird/NotificationListenerService-Example
Just tested my app on new Android 5.0 and found that it have some bug in switching ringer mode via Audio Manager. After set RINGER_MODE_SILENT it comes to "Allow only priority interruptions" mode and it's ok, it's how described in what's new document. But after set RINGER_MODE_NORMAL phone doesn't come back to "Always interrupt" and this is not expected behavior. User can miss the call because of it.
Does someone find solution/work around this problem? How can I turn off this filtration mode?
Update
Found this code in Android src. This settings is Global.ZEN_MODE. And code that should change it on set NORMAL_MODE looks like correct, but it doesn't work in Emulator and Nexus ROM. Had try to set via Settings.Global.putInt, but got error about permissions. Have no idea how to fix it =(
Made bug report: https://code.google.com/p/android/issues/detail?id=78158&thanks=78158&ts=1414182304
And in preview tracker https://code.google.com/p/android-developer-preview/issues/detail?id=1780&thanks=1780&ts=1414218141
Just found some work around. We can use NotificationListenerService.requestInterruptionFilter to change filtration mode. It works, but you have to add your service in "Notification access" list (it's in Sound & notification settings), otherwise you will have no permissions.
I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.
I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.
Possible solutions are rooting the new phone and thus making advanced settings visible
or increasing the time-out (which i dont like so much as a solution).
Do you have any ideas how to solve the issue or general input that might enlighten me?
Thnx!
BTW: here is the app in question (no ad):
Google Play Link
I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)
First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.
But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();
...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.
Then, when I finish needing to keep the phone awake:
mWakeLock.release();
You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>