Reputation: 4397
I am using the official android watch face API and I want to keep the screen on for a couple of seconds during an animation so that the screen doesn't go into ambient mode during the animation and once the animation is finished, I want to reset everything back to normal, is this possible? My class extends CanvasWatchFaceService and I am also extending the CanvasWatchFaceService.Engine So I want something similar to this but for a watchface:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Then this:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
Upvotes: 4
Views: 1956
Reputation: 42168
You need to explicitly hold wake lock: http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
Get a wake lock that keeps the screen on and release it when you finish animating.
Here is a document about keeping the device awake: https://developer.android.com/training/scheduling/wakelock.html
And here is an example:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
"WatchFaceWakelockTag"); // note WakeLock spelling
wakeLock.acquire();
Upvotes: 3
Reputation: 3493
You can also use the "setKeepScreenOn(true)" method to keep the screen on, lets say you have a (Root) RelativeLayout in which your animation takes place :
private RelativeLayout rLayout;
In OnCreate() get the wanted layout:
final WatchViewStub stub = (WatchViewStub)
findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new
WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
// rLayout is the root layout in which your animation takes place
rLayout = (RelativeLayout) stub.
findViewById(R.id.your_layout);
relativeLayout.setKeepScreenOn(true);
Then you can set an AnimationListener on your animation to setKeepScreenOn(false) again :
yourAnimation.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if(animation.hasEnded())
rLayout.setKeepScreenOn(false);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
Upvotes: 0