I have this ToggleButton that is enabled when a certain condition (Website content) is true.
getSystemOnState(..) connects to a webserver and this causes an exception because of strict mode. What is wrong about the way I am using the Handler class?
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();
}
}