2011-07-12 33 views
2

嘿,我正在開發一個android應用程序,我想連接到該應用程序內的網頁。不過,我已經嘗試過WebView的一些擴展,但它的顯示文件在我的目錄上很好,但是當連接到google.com時,它顯示錯誤!如何在不打開瀏覽器的情況下在我們的android應用程序中查看網頁?

然後我說這個文件 <uses-permission android:name="android.permission.INTERNET" />

在我的Manifest.xml

現在的URL(google.com)被顯示在瀏覽器 任何幫助,我怎麼能打開我的應用程序內的瀏覽器?

+0

你使用Javascript功能來指定允許嗎? –

+0

請更新您已經試過的代碼 –

+0

y我已啓用javascript! – Anuj

回答

7

使用webview。

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

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

     mWebView = (WebView) findViewById(R.id.webView); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://www.google.com/"); 
    } 
} 

在清單文件中添加<uses-permission android:name="android.permission.INTERNET" />

這裏使用的main.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<WebView 
     android:id="@+id/webView" 
     android:scrollbars="none" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    /> 
</LinearLayout> 
+0

試一試這也會出現同樣的問題,我重定向到瀏覽器,一旦我重定向我可以回到我的應用程序! – Anuj

+0

我不明白你想說什麼?請你可以解釋更多 –

+0

好吧,基本上我正在開發一個Android應用程序,我想在我的應用程序中打開一個網頁,這樣我就可以在任何時候關閉它,並且可以返回到我的應用程序,但使用webview的網頁越來越多顯示在外部瀏覽器中,我發現沒有辦法從它返回到我的應用程序......... – Anuj

0

您可以使用webview。你說什麼,它不顯示谷歌。也許問題是在其他地方。顯示谷歌時沒有任何錯誤的方式。爲了顯示網頁的目的,Webview是當然的。

+0

與webview它顯示google.com,但在瀏覽器,而我想網頁顯示在應用程序本身內。 – Anuj

1

它必須有用到u ..它不會在瀏覽器中加載..

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
import android.widget.EditText; 

public class WebViewDemo extends Activity { 
    private class MyWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
    private WebView webView; 
    private EditText urlField; 

    private Button goButton; 

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

     // Create reference to UI elements 
     webView = (WebView) findViewById(R.id.webview_compontent); 
     urlField = (EditText)findViewById(R.id.url); 
     goButton = (Button)findViewById(R.id.go_button); 

     // workaround so that the default browser doesn't take over 
     webView.setWebViewClient(new MyWebViewClient()); 

     // Setup click listener 
     goButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       openURL(); 
      } 
     }); 

     // Setup key listener 
     urlField.setOnKeyListener(new OnKeyListener() { 
      public boolean onKey(View view, int keyCode, KeyEvent event) { 
       if(keyCode==KeyEvent.KEYCODE_ENTER) { 
        openURL(); 
        return true; 
       } else { 
        return false; 
       } 
      } 
     }); 

    } 

    /** Opens the URL in a browser */ 
    private void openURL() { 
     webView.loadUrl(urlField.getText().toString()); 
     webView.requestFocus(); 
    }  
} 

xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
     android:id="@+id/url" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"  
     android:lines="1" 
     android:layout_weight="1.0" android:hint="http://"/> 

     <Button 
     android:id="@+id/go_button" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"  
     android:text="@string/go_button" 
     /> 

    </LinearLayout> 

    <WebView 
     android:id="@+id/webview_compontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1.0" 
    /> 
</LinearLayout> 

和mainfiest文件不要忘了給上網權限.. 我希望這可以幫助你..

+1

已經嘗試過,但網頁在瀏覽器中打開這個,而我的問題是加載應用程序內的網頁 – Anuj

+0

不,它不會在瀏覽器中加載..你只需在xml中減少webview的高度寬度..你會看到它的加載,特別是端口,,, –

0

另一種方法是通過Intents:

Uri uri = Uri.parse("http://www.gmail.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

你需要在manifest文件

<uses-permission android:name="android.permission.INTERNET" /> 
相關問題