0
I have an application in which you have a webview, so I created a preloader to load into a thread , now I need to put the webview in another thread , how can I do (code error ) ?
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.ConnectivityManager;
import android.content.Context;
import android.net.NetworkInfo;
import android.net.Network;
import android.widget.ProgressBar;
public class ConectActivity extends Activity {
private WebView webView;
private ProgressBar progress;
public boolean verificaConexao() {
boolean conectado;
ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
conectado = true;
} else {
conectado = false;
}
return conectado;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conect);
// *** roda qnd abre - Augusto Furlan ***
Boolean conect = verificaConexao();
String url = "http://google.com";
webView = (WebView) findViewById(R.id.webView);
progress = (ProgressBar) findViewById(R.id.progress);
//webView.setWebViewClient(new CustomWebViewClient());
webView.setVisibility(webView.GONE);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setSupportZoom(true);
webView.setWebViewClient(new WebViewClient());
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(25000);
runOnUiThread(new Runnable() {
@Override
public void run() {
showWebView();
}
});
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
if(self.conect == true) {
webView.loadUrl(self.url);
} else {webView.loadUrl("file:///android_asset/not-found.html");;}
runOnUiThread(new Runnable() {
@Override
public void run() {
showWebView();
}
});
}
}).start();
}
private void showWebView() {
webView.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
}
}
Why didn’t you put the code here instead of using Pastebin?
– Jéf Bueno
because it was already there , for being more practical, but I can put the code here, 1 min
– Augusto Furlan
What is the mistake? I suggest that Edit your question and highlight what the error is.
– emanuelsn
at the time within the second thread it does not allow me to use self.url , does not allow access to global
– Augusto Furlan
But not everyone who can help you always has access to those links. It is recommended to always post the source of the error right here and, for long codes, post on the external link for those who want more details of your code.
– user28595
OK sorry, already edited and put the code
– Augusto Furlan
I have some variables and I need to access to perform the comparison
– Augusto Furlan
See if my answer answers to what you want.
– ramaral