2013-04-06 80 views
0

URL HERE如何在android應用程序中使用webview作爲瀏覽器?

當我在我的應用程序的WebView加載上面的網址將其更改爲http://m.allrecipes.com但是,當我在瀏覽器的URL加載相同的URL http://allrecipes.com

有沒有辦法在我的應用程序的WebView加載正常的URL(http://allrecipes.com)不加載頁面移動(http://m.allrecipes.com

有人知道請幫我

go.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (Searchtext.getText().toString().equalsIgnoreCase("")) { 

        alertDialog = new AlertDialog.Builder(OnlineRecipe.this) 
          .create(); 
        alertDialog.setTitle("Message"); 
        alertDialog.setMessage("Please Enter Some Word"); 

        alertDialog.setButton("OK", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // Write your code here to execute after 
            // dialog closed 
            alertDialog.dismiss(); 
           } 
          }); 

        // Showing Alert Message 
        alertDialog.show(); 
       } else { 

        String url = "http://allrecipes.com/search/default.aspx?qt=k&wt=" 
          + Searchtext.getText().toString() 
          + "&rt=r&origin=Recipe Search Results"; 
        webview.getSettings().setJavaScriptEnabled(true); 
        webview.loadUrl(url); 


       } 



      } 
     }); 

回答

4

如果我理解正確的,所有你想要做的是負載http://allrecipes.com在桌面模式下你的WebView ......如果是這樣的話,使用下面的代碼來得到它的工作:

webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/20 Safari/537.31"); 

這隻需要在創建WebView對象webview的部分之後放在代碼中。

編輯:只是讓你知道以供將來參考,這行代碼更改了用戶代理欺騙網站,認爲你是從計算機而不是電話訪問它。

0

不記得添加活動您:AndroidManifest.xml中

例子:

<activity android:name=".Second" 
       android:label="@string/app_name" 
       android:theme="@android:style/Theme.NoTitleBar" 
       android:configChanges="orientation|keyboardHidden"> 
    </activity> 

類:

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class Second extends Activity { 

    WebView webView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webView = (WebView)findViewById(R.id.webview); 

     webView.getSettings().setJavaScriptEnabled(true); 

     webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

     webView.setWebViewClient(new WebClient()); 

     webView.loadUrl(getIntent().getExtras().getString("url")); 

    } 



    public class WebClient extends WebViewClient 
    { 
     ProgressDialog pd; // Create Proggress Dialog to show if User Internet connection is slow 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      Intent i = new Intent(Second.this, Second.class); 
      i.putExtra("url", url); 
      startActivity(i); 

      return true; 
     } 

     public void onPageStarted(WebView view, String url, Bitmap favicon) 
     { 
      pd = ProgressDialog.show(Second.this, "",getString(R.string.loading), true); 
      pd.setCancelable(true); 
     } 

     public void onPageFinished(WebView view, String url) 
     { 
      if (pd.isShowing()) { 
       pd.dismiss(); 
      } 
     } 
    } 
} 
+0

謝謝你的回覆,但我不是這個意思。當我想在webview中加載上面的url時,它會變成手機兼容的網址爲「m.allrecipes.com」我不想要這個,我也不想要任何新的活動 – Nas 2013-04-06 07:00:01

+0

您必須爲每個頁面開始一個新的活動。 webView.loadUrl(getIntent()。getExtras()。getString(「url」)); - 在這裏你可以把靜態的網頁。也許:m.allrecipes.com – 2013-04-06 07:09:09

+0

@ d.danailov,這是不正確的。您可以輕鬆將Web視圖放入片段或活動中,並在每次需要新頁面時調用loadurl。 – cYrixmorten 2013-09-29 18:55:26

相關問題