简体   繁体   中英

Android Wear watchface keep screen on

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).

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();

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) {

       }
                    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM