2010-07-17 3 views
2

문맥을 필요로하는 타이머를 사용하고 있습니다. 지금 나는 다음과 같은 코드가 있습니다문맥을 타이머에 넘기는 것은 안전합니다

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(); 

이 사용해도 안전을, 또는 여기 컨텍스트를 유출 할 수있다? btw. 이것은 broadcastreceiver입니다.

답변

2

컨텍스트를 스레드로 전달하지 말고 대신 부모 이름으로 참조해야합니다. 이

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 
     } 
    }; 

... 

} 

같은

뭔가 그래서 당신은 아마 당신의 방송 수신기의 이름 등으로 컨텍스트를 호출해야합니다 MyReceiver.this.contextcontext 클래스의 구성원 인 가정.