我的应用程序有一个列表视图,其中包含来自 RSS 提要的数据。选择一行时,它在Web视图中显示该文章。它会加载页面,但不会像 Chrome 那样加载完整的移动网站。我将在我的应用程序的 Web 视图和 Chrome 中的同一页面中加载的文章的屏幕截图。有任何想法吗?
这是在 Chrome 中:
这是在我的网络视图中:
package com.example.kyfbtest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
public class WebBrowser extends Activity {
WebView web;
ProgressBar prgPageLoading;
ImageButton btnBack, btnForward;
Button btnShare;
String myURL;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webbrowser);
Intent iGet = getIntent();
myURL = iGet.getStringExtra("myURL");
web = (WebView) findViewById(R.id.web);
prgPageLoading = (ProgressBar) findViewById(R.id.prgPageLoading);
btnBack = (ImageButton) findViewById(R.id.btnBack);
btnForward = (ImageButton) findViewById(R.id.btnForward);
btnShare = (Button) findViewById(R.id.btnShare);
web.getSettings().setDomStorageEnabled(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setRenderPriority(RenderPriority.HIGH);
web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
web.loadUrl(myURL);
btnShare.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String webUrl = web.getUrl();
String webTitle = web.getTitle();
final String p0 = "KYFB Share:";
final String p1 = "Kentucky Farm Bureau";
final String p2 = "'Big On Commitment.'";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, webTitle);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, p0 + "\n\n" + webUrl + "\n\n\n" +
p1 + "\n" + p2 + "\n\n");
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
}
});
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(web.canGoBack()){
web.goBack();
} else
finish();
}
});
btnForward.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(web.canGoForward()){
web.goForward();
}
}
});
final Activity act = this;
web.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView webview, int progress){
act.setProgress(progress*100);
prgPageLoading.setProgress(progress);
}
});
web.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted( WebView view, String url, Bitmap favicon ) {
super.onPageStarted( web, url, favicon );
prgPageLoading.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished( WebView view, String url ) {
super.onPageFinished( web, url );
prgPageLoading.setProgress(0);
prgPageLoading.setVisibility(View.GONE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(act, description, Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i); //warning no error handling will cause force close if no media player on phone.
return true;
}
view.loadUrl(url);
return true;
}
});
}
}