views:

13

answers:

1

i'm using a timer wich needs context.. Now i have the following code :

   mContext=context;
    mEventID=eventID;
    CountDownTimer cdTimer = new CountDownTimer(20000, 1000) {
        public void onTick(long millisUntilFinished) {
            // Do nothing onTick... Sorry
        }
        public void onFinish() {
            int deletedRows = mContext.getContentResolver().delete()
            // ..rest of code
        }
    };
    cdTimer.start();

Is this safe to use , or may i be leaking the context here? btw. this is in a broadcastreceiver.

A: 

You should not pass the Context into the Thread, instead refer to it by it's parent name.

something like this

class MyClass {
...
CountDownTimer cdTimer = new CountDownTimer(20000, 1000) {
        public void onTick(long millisUntilFinished) {
            // Do nothing onTick... Sorry
        }
        public void onFinish() {
            int deletedRows = myClass.this.context.getContentResolver().delete()
            // ..rest of code
        }
    };

...

}

So you probably need to call the context by your broadcast receiver's name eg: MyReceiver.this.context assuming context is a member of the class.

Pentium10