2011-09-11 65 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