Android Wear not waking screen

旧城冷巷雨未停 提交于 2019-12-12 04:48:14

问题


I'm working on a watch face and its up and running but when I put it on my 360 it becomes slow to respond to interactions.

I should note that the screen wakes when a notification is received but not when one turns their wrist to look at the time.

Last thing I did was move code out of doWork and into CountDownRunner. Below is my code:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.support.wearable.view.WatchViewStub;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Date;

public class ClockActivity extends Activity {

private TextView mTextView;
private Handler mHandler = new Handler();
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clock);

    Runnable runnable = new CountDownRunner();
    Thread myThread = new Thread(runnable);
    myThread.start();

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
}

class CountDownRunner implements Runnable{
    public void run() {
        while(!Thread.currentThread().isInterrupted()){
            try {
                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                int seconds = dt.getSeconds();
                String curTime = hours + ":" + minutes + ":" + seconds;

                img=(ImageView)findViewById(R.id.hand_second);
                RotateAnimation rotateAnimation = new RotateAnimation((seconds-1)*6, seconds*6,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                        0.5f);

                rotateAnimation.setInterpolator(new LinearInterpolator());
                rotateAnimation.setDuration(1000);
                rotateAnimation.setFillAfter(true);

                img.startAnimation(rotateAnimation);
                runOnUiThread(new Runnable() {
                    public void run() {
                        try{

                        }catch (Exception e) {

                        }
                    }
                });
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }catch(Exception e){
            }
        }
    }
}

}

回答1:


You should not implement Runnable to change time on your wearable. Use BroadcastReceiver (Intent.ACTION_TIME_TICK, Intent.ACTION_TIMEZONE_CHANGED, Intent.ACTION_TIME_CHANGED) and receive a "tick" when time change (consumes less battery power and CPU).

Moreover, you must managed screen states (screen dim, screen awake, etc.)

Here is a great exemple of Watchface for Android Wear: http://www.binpress.com/tutorial/how-to-create-a-custom-android-wear-watch-face/120

And a last tips, you can manage seconds for your watchface only when your wearable is awake (like postdaleyed but must be killed when your wearable go back to sleep mode).



来源:https://stackoverflow.com/questions/26350122/android-wear-not-waking-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!