Code example for WeakReference

    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();
        public void onReceive(Context context, Intent intent) {
            // Post a runnable to avoid blocking the broadcast. 
            final boolean timezoneChanged =
            final DigitalClock clock = mClock.get();
            if (clock != null) {
       Runnable() {
                    public void run() { 
                        if (timezoneChanged) {
                            clock.mCalendar = Calendar.getInstance();
Contextual code suggestions in your IDE  Get Codota for Java