O WebView não está carregando página da web
estou usandoWebView
para carregar um site. Mas é muito lento e vaza quando sites específicos são carregados. Estou carregandoWebView
com o seguinte código.
@Override
protected void onNewIntent(Intent intent) {
if (intent.getStringExtra("url") != null) {
webView.loadurl(intent.getStringExtra("url"));
}
}
Mas eu estou ligandowebView.loadUrl(Config.URL);
(Config.URL
pode conter o mesmo URL especificado acima) emonCreate()
método após a inicializaçãoWebView
com o seguinte.
this.webView = (WebView) findViewById(R.id.wv);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setLoadsImagesAutomatically(true);
this.webView.getSettings().setDomStorageEnabled(true);
this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
MyClient client = new MyClient(WebActivity.this, (ProgressBar)findViewById(R.id.progressBar));
webView.setWebViewClient(client);
Carregando um deonCreate()
está funcionando bem (não está bem, é muito lento). Mas o mesmo URL que está sendo carregado deonNewIntent()
énão está funcionando!!!. Depois que eu fiz isso emonNewIntent()
nenhum URL foi carregado usandowebView.loadurl()
e a página atual está ficando imóvel. ie as barras de rolagem estão se movendoWebView
mas a página não está rolando. Eu testei o mesmo URL emonCreate()
e está funcionando.
Por fazer isso eu estou passando url com
intent.putExtra("url", Config.URL+targetUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
com a intenção pendente das notificações. Embora esteja funcionando em alguns dispositivos, como o Google Nexus. Mas não está funcionando na maioria dos telefones. eu tenho
android:hardwareAccelerated="true"
Meu cliente
public class MyClient extends WebViewClient{
private Context context;
private Activity activity;
private Handler handler;
private Runnable runnable;
private ProgressBar viewBar;
private String ret,ret2;
public void setFirstLoad(boolean firstLoad) {
this.firstLoad = firstLoad;
}
private boolean firstLoad=false;
public MyClient(Activity activity, ProgressBar bar) {
this.context = activity.getApplicationContext();
this.activity = activity;
viewBar=bar;
handler=new Handler();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/*if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
*//*view.setVisibility(View.GONE);
viewBar.setVisibility(View.VISIBLE);*//*
view.loadUrl(url);
}
return true;*/
if (Uri.parse(url).getHost().equals("www.somepage.com")) {
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Answers.getInstance().logShare(new ShareEvent()
.putContentId(Build.USER)
.putMethod(shareName(url))
.putContentName(contentDecode(url))
.putContentType("news_share"));
}catch (android.content.ActivityNotFoundException e){
Log.e("Activity not found",e.toString());
Toast.makeText(context,"Application not found",Toast.LENGTH_LONG).show();
}
return true;
}
@Override
public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl) {
//Clearing the WebView
try {
view.stopLoading();
} catch (Exception e) {
}
try {
view.clearView();
} catch (Exception e) {
}
if (view.canGoBack()) {
view.goBack();
}
view.loadUrl("about:blank");
//Showing and creating an alet dialog
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
alertDialog.setTitle("Error");
alertDialog.setMessage("No internet connection was found!");
alertDialog.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
view.loadUrl(failingUrl);
}
});
AlertDialog alert = alertDialog.create();
alert.show();
//Don't forget to call supper!
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onLoadResource(final WebView view, String url) {
super.onLoadResource(view, url);
//injectScriptFile(view, "js/script.js");
injectCSS(view,"css/style.css");
if (firstLoad){
firstLoad=false;
view.setVisibility(View.INVISIBLE);
viewBar.setVisibility(View.VISIBLE);
runnable=new Runnable() {
@Override
public void run() {
viewBar.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
}
};
handler.postDelayed(runnable,2000);
}
// test if the script was loaded
// view.loadUrl("javascript:setTimeout(hideMe(), 200)");
}
/*@Override
public void onPageFinished(final WebView view, String url) {
//System.gc();
}*/
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.gc();
}
A questão é:Qual é o problema ao usarloadurl()
método emonNewIntent()
?