8

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?

5
  • Could you please give us more details about how exactly you tried to keep device awake?
    – Okas
    Commented Sep 17, 2014 at 9:38
  • Tried ACQUIRE_CAUSES_WAKEUP?
    – Umair
    Commented Sep 21, 2014 at 20:48
  • Any news, I am also looking for something similar, but cannot find any solution.
    – akmal
    Commented Oct 5, 2014 at 10:10
  • 2
    I found a solution using AlarmManager with AlarmManager.ELAPSED_REALTIME_WAKEUP to send a broadcast. Launch a notification or send data to phone inside onReceive. No need to use Wakelock. Note: AlarmManager.RTC_WAKEUP did not work for my case.
    – taingmeng
    Commented Oct 7, 2014 at 2:00
  • The solution with the Alarm manager and the WakefulBroadcastReceiver seems to be the correct one. You have a concrete example here: github.com/AlexKorovyansky/WearPomodoro
    – nicolas
    Commented Dec 8, 2014 at 13:03

2 Answers 2

3

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
  • can you please tell me In which Manifest file(phone or wear) we have to specify the WAKE LOck permission?Because i tried in wearable manifest file after that phone stopped pushing the app to wearable
    – Vny Kumar
    Commented Nov 16, 2014 at 11:40
  • Instead of extending BroadcastReceiver, why don't you use a WakefulBroadcastReceiver ? Commented Nov 24, 2014 at 10:56
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.