How To Wake Up Android Wear When It Is In Sleep Mode?
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 w
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
publicabstractclassWatchFaceActivityextendsActivity {
private PowerManager.WakeLock mWakeLock;
private Handler mWakeLockHandler;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock_watch_face);
PowerManagerpowerManager= (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MyWakelockTag");
mWakeLockHandler = newHandler();
IntentFiltermessageFilter=newIntentFilter("message-forwarded-from-data-layer");
MessageReceivermessageReceiver=newMessageReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
}
publicclassMessageReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
if (!mWakeLock.isHeld()) {
mWakeLock.acquire();
}
mWakeLockHandler.removeCallbacksAndMessages(null);
mWakeLockHandler.postDelayed(mReleaseRunnable, 5000);
}
}
privateRunnablemReleaseRunnable=newRunnable() {
@Overridepublicvoidrun() {
mWakeLock.release();
}
};
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
mWakeLockHandler.removeCallbacksAndMessages(null);
mWakeLock.release();
}
}
And allow WAKE_UP permission in your Manifest.
<uses-permissionandroid:name="android.permission.WAKE_LOCK" />
Solution 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.
Post a Comment for "How To Wake Up Android Wear When It Is In Sleep Mode?"