Skip to content

Instantly share code, notes, and snippets.

@egglang

egglang/1-2.java Secret

Created January 23, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egglang/8862bc8ed7131dc5b847dcd5680a2d4e to your computer and use it in GitHub Desktop.
Save egglang/8862bc8ed7131dc5b847dcd5680a2d4e to your computer and use it in GitHub Desktop.
課題1 MainActivityの実装例
private TextView mText;
private ClockLegacy.ClockListener mListener = new ClockLegacy.ClockListener() {
@Override
public void onReceive(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String dateString = String.format("%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
mText.setText(dateString);
Log.d("CODELAB", “The current time is " + dateString);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..
mText = findViewById(R.id.text);
mClockData = new ClockLegacy(getApplicationContext());
final LifecycleObserver observer = new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void calledWhenOnStart(LifecycleOwner source) {
mClockData.setClockListener(mListener);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void calledWhenOnStop(LifecycleOwner source) {
mClockData.removeClockListener();
}
};
getLifecycle().addObserver(observer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment