Reputation: 31
Recently I'm working on App, that show video stream from URL. After connecting to specified WiFi it should open WebView, and stream Video.
Problem is, that I need to check if we are using proper WiFi, then switch to new Activity. Listener, that checks it is creating own thread, and can't modify UI. I've tried runOnUiThread, but it still don't change the UI, I just get rid of Exceptions. Is there a way to make it working? My code:
public class MainActivity extends AppCompatActivity {
...
private Boolean mStatus = Boolean.FALSE;
private ConnectionHandler mConnectionStatus;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mConnectionStatus = new ConnectionHandler();
...
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLoadingScreen();
connectToCamera(mWifiManager);
new Thread(new ConnectivityStatus()).start();
}
});
mConnectionStatus.addConnectivityListener(new ConnectionHandler.ConnectivityListener() {
@Override
public void onChange() {
runOnUiThread(new Runnable() {
@Override
public void run() {
stopLoadingScreen();
openWebView();
}
});
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(!mConnectionStatus.getStatus()){
stopLoadingScreen();
Toast.makeText(getApplicationContext(),
"Camera not found.\nMake sure that camera is turned on.",
Toast.LENGTH_LONG).show();
}
}
}, 30000);
}
public void stopLoadingScreen() {
connectButton.setAlpha(1.0f);
mTextView.setAlpha(1.0f);
mLoadingView.setVisibility(View.GONE);
}
...
private void openWebView() {
Intent intent = new Intent(this, WebViewActivity.class);
startActivity(intent);
}
class ConnectivityStatus implements Runnable{
@Override
public void run(){
long startTime = System.currentTimeMillis();
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
mPermissions = Boolean.FALSE;
}
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
while ((System.currentTimeMillis() - startTime) < 30000) {
if (wifiInfo.getSSID().equals(mSsid)) {
mConnectionStatus.setStatus(Boolean.TRUE);
break;
}
}
}
}
}
}
Upvotes: 1
Views: 35
Reputation: 1689
Try to use post()
method from Handler. For example:
new Handler().post(runnable)
Upvotes: 1