2011-09-11 173 views
0

我想在我的活动中制作一个简单的WebView,并且在虚拟设备上的我的android浏览器以及实际设备上不断收到“网页不可用”页面。webView android

我看过网上的一些样本以及我书中的例子,但我相信还是有一些东西丢失了。任何帮助,将不胜感激。代码如下。 wv正在引用main.xml文件中的webview对象。

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     WebView wv = (WebView) findViewById(R.id.webView1); 
     WebSettings webSettings = wv.getSettings(); 
     wv.getSettings().setJavaScriptEnabled(true); 
     webSettings.setBuiltInZoomControls(true); 
     wv.loadUrl("http://www.amazon.com"); 
    } 
} 
+1

您是否已将“INTERNET”权限添加到您的Android清单? –

+0

我没有!我现在就试一试。 – JCC

+0

哇!我不能相信我错过了这个细节......这是第1步:/感谢您的帮助它做到了这个把戏:) – JCC

回答

3

对于Android的web视图查看我的应用程序你需要记住4件事情,使其工作完美。

  1. 提供必要的许可访问互联网在你的Android 的Manifest.xml
  2. 进口必要的库像WebClient的webchromeclientYourActivity.java
  3. 启用JavaScript的如果您要显示需要Java支持的网页。
  4. 将您在webView中使用的所有图像文件复制并粘贴到项目的“res”文件夹中的所有“可绘制”文件夹中。

我已经给你从Manifest到Activity.java的完整代码。它工作完美。祝一切顺利..!

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.yourappname" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="21" /> 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permissions.NETWORK_ACCESS" /> 
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/yourapplogo" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <WebView 
     android:id="@+id/webView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

MainActivity.java:

package com.example.yourappname; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.webkit.ValueCallback; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.webkit.WebChromeClient; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

WebView web; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    web = (WebView) findViewById(R.id.webView1); 
    web = new WebView(this); 
    web.getSettings().setJavaScriptEnabled(true); 
    web.loadUrl("http://www.yourwebsite.com"); 
    web.setWebViewClient(new myWebClient()); 
    web.setWebChromeClient(new WebChromeClient()); 
    setContentView(web); 
} 



public class myWebClient extends WebViewClient 
{ 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     // TODO Auto-generated method stub 
     super.onPageStarted(view, url, favicon); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 

     view.loadUrl(url); 
     return true; 

    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     // TODO Auto-generated method stub 
     super.onPageFinished(view, url); 


    } 
} 

//flip screen not loading again 
@Override 
public void onConfigurationChanged(Configuration newConfig){   
    super.onConfigurationChanged(newConfig); 
} 



@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(event.getAction() == KeyEvent.ACTION_DOWN){ 
     switch(keyCode) 
     { 
     case KeyEvent.KEYCODE_BACK: 
      if(web.canGoBack()){ 
       web.goBack(); 
      } 
      else 
      { 
       backButtonHandler(); 
      } 
      return true; 
     } 

    } 
    return super.onKeyDown(keyCode, event); 
} 
public void backButtonHandler() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(
       MainActivity.this); 


     // Setting Dialog Title 
     // Setting Dialog Message 

     alertDialog.setTitle("Your App Name"); 

    // I've included a simple dialog icon in my project named "dialog_icon", which's image file is copied and pasted in all "drawable" folders of "res" folders of the project. You can include any dialog image of your wish and rename it to dialog_icon. 

     alertDialog.setIcon(R.drawable.dialog_icon); 
     alertDialog.setMessage("Exit Now?"); 

     // Setting Icon to Dialog 
     // Setting Positive "Yes" Button 

     alertDialog.setPositiveButton("Exit", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
        } 
     }); 

    // Setting Negative "NO" Button 

     alertDialog.setNegativeButton("No", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // Write your code here to invoke NO event 
         dialog.cancel(); 
        } 
       }); 
    // Showing Alert Message 
     alertDialog.show(); 
} 

} 
+0

唯一的工作解决方案,我thanx很多:) – Zub

+0

我很高兴它适合你'ZUB' – sam