-
-
Save egglang/8862bc8ed7131dc5b847dcd5680a2d4e to your computer and use it in GitHub Desktop.
課題1 MainActivityの実装例
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
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