How to wake up Android Wear when it is in sleep mode?

社会主义新天地 提交于 2019-12-09 11:32:48

问题


When Android Wear goes to sleep mode (screen dimmed), some parts of my code are not executed. I use Timer in background service to trigger some actions, such as sending data from wear to mobile, but the data is not sent. It is sent when I tap the screen to wake it up.

I also try to use Timer trigger a notification with vibration when the screen is off, but it doesn't appear until I tap the screen.

In debug mode (either Bluetooth or USB), data sending and notification work fine.

I suspect this is because when Android Wear is in sleep mode, its CPU works at minimum level because the Timer is still running, but not for GoogleApiClient, IntentService, or Notification.

I have tried many ways to wake CPU up such as AlarmManager, PowerManager, Wakelock, but it did not work for Android Wear.

Anyone has encountered this problem? What is the solution?


回答1:


I'm using PowerManger to wakeup my wearable device each time i receive message from handled device. Do not forget to release PowerManager.WakeLock

public abstract class WatchFaceActivity extends Activity {

    private PowerManager.WakeLock mWakeLock;
    private Handler mWakeLockHandler;

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

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MyWakelockTag");

        mWakeLockHandler = new Handler();

        IntentFilter messageFilter = new IntentFilter("message-forwarded-from-data-layer");
        MessageReceiver messageReceiver = new MessageReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!mWakeLock.isHeld()) {
                mWakeLock.acquire();
            }
            mWakeLockHandler.removeCallbacksAndMessages(null);
            mWakeLockHandler.postDelayed(mReleaseRunnable, 5000);
        }
    }

    private Runnable mReleaseRunnable = new Runnable() {

        @Override
        public void run() {
            mWakeLock.release();
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWakeLockHandler.removeCallbacksAndMessages(null);
        mWakeLock.release();
    }
}

And allow WAKE_UP permission in your Manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" />



回答2:


You should use AlarmManager along with WakefulBroadcastReceiver and startWakefulService(). See this working solution.
You may find answers for your further questions in chat history on that post here.This is the only solution worked for our app.

@SeaDog is successful in making http calls when device in deep sleep mode with this solution. Try it.



来源:https://stackoverflow.com/questions/25886935/how-to-wake-up-android-wear-when-it-is-in-sleep-mode

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