Android 将上下文传递到计时器..这安全吗
我使用的计时器需要上下文。 现在我有以下代码:Android 将上下文传递到计时器..这安全吗,android,android-context,Android,Android Context,我使用的计时器需要上下文。 现在我有以下代码: mContext=context; mEventID=eventID; CountDownTimer cdTimer = new CountDownTimer(20000, 1000) { public void onTick(long millisUntilFinished) { // Do nothing onTick... Sorry } publi
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();
这是安全的使用,还是我可以在这里泄漏上下文?
顺便说一句,这是在一个
广播接收器中
您不应该将上下文传递到线程中,而是通过它的父名称来引用它
像这样的
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.context
假设context
是该类的成员