当某个条件(网站内容)为真时,我启用了这个 ToggleButton。
getSystemOnState(..) 连接到网络服务器,由于严格模式,这会导致异常。我使用 Handler 类的方式有什么问题?
public class ReceiverToggleButton extends ToggleButton {
private Runnable mTicker;
private Handler mHandler;
private boolean mTickerStopped = false;
private String rxhost = null;
private Context context = null;
public ReceiverToggleButton(Context context) {
super(context);
this.context = context;
updateOnOffState(context);
}
private void updateOnOffState(final Context cxt) {
Runnable r = new Runnable() {
public void run() {
rxhost = cxt.getResources().getString(R.string.host_receiver);
mHandler = new Handler();
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) {
return;
}
boolean isSystemOn = getSystemOnState(rxhost); // connects to webserver
setChecked(isSystemOn);
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + 1000 * 10; // check every 10s
mHandler.postAtTime(this, next);
}
};
mHandler.post(mTicker);
}
};
new Thread(r).start();
}
}