How to disable/enable screen rotation from an android SERVICE? - android

During some period that an android service is running the screen rotation needs to be disabled.
Is there any way to disable/enable the screen rotation from an android service?

You can imagine that what may happen if service controls the mode of device - there may be an app's activity who requested or need a landscape mode because user has rotated the device but the service (which is running in background) which user has no idea, is forcing the portrait mode for the device, even though user rotated device for landscape experience - not good for overall user experience.
Hence, AFAIK, the android:screenOrientation option which controls the landscape/portrait mode, is applicable to Activity class only & not to the Service.
Since Service's job is to run in background it typically does not contain any UI & hence options such as how the UI elements are to be display i.e in landscape or portrait mode is not the role of Service.

Related

android service foreground screen lock mode

I need to detect when the android smartphone goes into lock screen mode and when it comes out.
I need it to be able to bring a service respectively in start foreground and stop foreground so that the location sensor continues to work both when the smartphone is off (on lock screen) and when it is on.
Is there a fast way to do this detection without having to use the kastri library?
Prefer not to be dependent on the kastri library. That's why I asked for this.

Can't simulate sleep mode on Android emulator

I am trying to put my emulators into sleep mode to test some behavior.
For this, I deactivate the charger, set the battery to "uncharging" and a low battery %. Then I lock the screen over the power button (swipe to unlock is activated).
Then I start some background operation (for example an IntentService) without holding a wake lock and leave the app, but everything just keeps running as if the screen was on.
The emulator seems to not fall into sleep.
Not a duplicate since I am already following these exact steps (as described above)

Android Wear - Show watchface while ambient, app while active

I'm designing an android app for my wearable device in which I have overridden ambient mode. I would like for my app to show the watchface while in ambient mode, and show the app while out of ambient mode. In other words, I want to make the app "invisible" while ambient mode is on. (Note that keeping the activity in the foreground while this is happening is desirable!)
I have looked but haven't found a way to show the watch face while the app is open. Can anyone direct me to some sources/provide an explanation as to how one might accomplish this?
I'm not sure why you want to keep the activity in the foreground when the watch is in ambient mode, but I think there's a better way to do it:
1) If you want your app to keep doing something (read from sensors etc) while in ambient mode, use a Service instead.
2) If you just want to keep the state of your activity, save it to a Bundle and use that to repopulate the UI when the app is launched again.
Keeping the activity alive when it's not shown is a waste of both memory and battery.
I suggest changing your activity to only show in interactive mode. Let the system kill it when the device goes into ambient mode. Then your watch face will be displayed automatically. You can launch the app again from your watch face when interactive mode is triggered.

Draw view before screen off or while screen off

Target: sleep screen for e-ink device.
Setup: e-ink device with android 4.4.2, firmware sources not available.
What I've tried already:
1) Overlay with zero brightness
add receiver for android.intent.action.SCREEN_OFF/android.intent.action.SCREEN_ON;
on screen off add fullscreen view in WindowManager with flags 'FLAG_TURN_SCREEN_ON', 'FLAG_KEEP_SCREEN_ON' and brightness set to '0';
Problems: device is not sleeping since screen is on, flicker since we're preventing device from going to sleep;
2) Simulate POWER_KEY press
add receiver for android.intent.action.SCREEN_OFF/android.intent.action.SCREEN_ON;
start fullscreen activity with flag 'FLAG_TURN_SCREEN_ON';
after activity is started simulate send POWER_KEY press with 'input keyevent 26';
Problems: root needed, flicker since we're preventing device from going to sleep;
What I want:
Solution that will not require root and will put device to sleep.
On the second thought - it seems not quite possible as it is.
Because, looking at the logs I saw that SurfaceFlinger is releasing screen before ActivityManager displays my activity. So, activity is actually get rendered it's just the screen won't update since system already received sleep-signal.
Maybe this could be figured out with NDK and fw sources.

How do I force screen on (if it's sleeping), in my Activity?

I am writing an Android alarm-like app.
I want to let the user to choose if to always keep the screen on for the whole application duration, or if she want it to go to sleep according to her device power manager settings.
The first scenario is correctly handled with this code:
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
But - in the second scenario - when the screen has been swichted off by power manager, I can play a sound via AudioManager, but I can't force the screen to switch on...
I'm using Build.VERSION.SDK = 10 and testing on a Samsung device with Android 2.3.4.
You can maintain a WakeLock, and acquire the WakeLock when you want to turn the screen on.
You can check out WakeLock here.

Categories

Resources