如何自动更改webview?

时间:2013-09-02 07:50:05

标签: android url dynamic webview

问题是url每天都在变化 来自

  

https://www.aquaserv.ro/index.php?npage=757

  

https://www.aquaserv.ro/index.php?npage=758

依旧......

package com.example.comunicatavariicompaniaaquaserv;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;  

public class tutorialOne extends Activity
{
final Activity activity = this;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.avarii);
    WebView webView = (WebView) findViewById(R.id.webview1);
    webView.getSettings().setJavaScriptEnabled(true);//enable java content
    webView.getSettings().setUseWideViewPort(false);//enable zoom-out page
    webView.getSettings().setDomStorageEnabled(true);


    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Se incarca datele...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }

    });

    webView.setWebViewClient(new WebViewClient() {


        @Override
        public void onReceivedError(WebView view, int errorCode, String description,   
        String failingUrl)                                                          
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
        });

    webView.loadUrl("https://www.aquaserv.ro/index.php?npage=757");
    } 
    }

3 个答案:

答案 0 :(得分:1)

你的用例是什么?你如何得到这个URL?你需要硬编码URL吗?什么触发了变化?

我建议您动态获取URL并将其传递给WebView。如果您知道URL何时更改,或者您始终可以获取具有唯一标识的最新URL,则可以在获取最新URL后在WebView中加载URL。

答案 1 :(得分:0)

WebView上的

loadUrl(String url)和load *(..)是允许您动态设置url的函数。以下活动可以做到。它有两个按钮,"点击进入谷歌"和"点击进入雅虎" ,单击时,调用loadUrl()方法以更新WebView。

因此,对于url中的任何更改,请使用参数调用loadUrl()函数,然后就完成了。

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }*/
        final WebView webView=(WebView)findViewById(R.id.webview);


        webView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        Button b= (Button)  findViewById(R.id.google);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                webView.loadUrl("http://www.google.com");

            }
        });
        Button b2= (Button) findViewById(R.id.yahoo);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                webView.loadUrl("http://www.yahoo.com");

            }
        });

    }

}

答案 2 :(得分:0)

使用此代码

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url.replace("https://www.aquaserv.ro/index.php?npage=757", "https://www.aquaserv.ro/index.php?npage=758"));
            return false;
        }