-
-
Save egglang/319231352e5538bfb9641634480aee04 to your computer and use it in GitHub Desktop.
課題2 ClockLiveData
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
import android.arch.lifecycle.LiveData; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.util.Log; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class ClockLiveData extends LiveData<Date> { | |
private final Context mAppContext; | |
private BroadcastReceiver mTimeTickBroadcastReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
// ... | |
} | |
}; | |
public ClockLiveData(Context appContext) { | |
mAppContext = appContext; | |
} | |
@Override | |
protected void onActive() { | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction(Intent.ACTION_TIME_TICK); | |
mAppContext.registerReceiver(mTimeTickBroadcastReceiver, intentFilter); | |
} | |
@Override | |
protected void onInactive() { | |
mAppContext.unregisterReceiver(mTimeTickBroadcastReceiver); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment