Created
December 1, 2017 01:58
-
-
Save daeun1012/560012101f9954747047b0e8bc24cb7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyLocationListener implements LifecycleObserver { | |
private boolean enabled = false; | |
public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) { | |
... | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_START) // lifecycle event : onStart() | |
void start() { | |
if (enabled) { | |
// connect | |
} | |
} | |
public void enable() { | |
enabled = true; | |
if (lifecycle.getCurrentState().isAtLeast(STARTED)) { | |
// connect if not connected | |
} | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) // lifecycle event : onStop() | |
void stop() { | |
// disconnect if connected | |
} | |
} | |
class MyActivity extends AppCompatActivity { | |
private MyLocationListener myLocationListener; | |
public void onCreate(...) { | |
myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { | |
// update UI | |
}); | |
Util.checkUserStatus(result -> { | |
if (result) { | |
myLocationListener.enable(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment