Code example for WeakReference

0
    private static class TimeChangedReceiver extends BroadcastReceiver {
        private WeakReference<DigitalClock> mClock;
        private Context mContext;
 
        public TimeChangedReceiver(DigitalClock clock) {
            mClock = new WeakReference<DigitalClock>(clock);
            mContext = clock.getContext();
        } 
 
        @Override 
        public void onReceive(Context context, Intent intent) {
            // Post a runnable to avoid blocking the broadcast. 
            final boolean timezoneChanged =
                    intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED);
            final DigitalClock clock = mClock.get();
            if (clock != null) {
                clock.mHandler.post(new Runnable() {
                    public void run() { 
                        if (timezoneChanged) {
                            clock.mCalendar = Calendar.getInstance();
                        }