正确保存共享偏好中的webview网址

时间:2016-01-28 06:03:26

标签: android webview

我的代码使用onpagefinished保存上次访问过的网址,但我想在点击按钮时执行此操作。我试过这个,但它没有保存任何东西,我的webview每次加载默认页面。我该如何让它发挥作用?

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

    checkConnectivity();

    webView = (WebView) findViewById(R.id.webView1);
    webView.setWebChromeClient(new MyWebViewClient());

    CookieManager.getInstance().setAcceptCookie(true);// Enable Cookies
    webView.getSettings().setJavaScriptEnabled(true);// Enable Java Script
    webView.setWebViewClient(new HelloWebViewClient());

    // mWebView.loadUrl("http://www.afreesms.com.prx2.unblocksit.es/freesms/");
    // Load the URL from saved url
    // webView.loadUrl("http://afreesms.com/worldwide/");
    webView.loadUrl(getUrl());
    webView.setPadding(0, 0, 0, 0);// fit on screen
    webView.setInitialScale(getScale());// fit on screen

    // zoom support
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);

    webView.getSettings().setLoadsImagesAutomatically(true);
}

private class MyWebViewClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int progress) {
        if (progress >= 86) { // 86%
            webView.stopLoading();
            // do screenshot
        }
    }
}

class HelloWebViewClient extends WebViewClient {

    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
        // Removes element which id = 'mastHead'
        view.loadUrl("javascript:(function() { "
                + "(elem = document.getElementById('header')).parentNode.removeChild(elem); "
                + "})()");
    }

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

    @Override
    // Loading Spinner and status
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        checkConnectivity();
        super.onPageStarted(view, url, favicon);
        findViewById(R.id.pb1).setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        findViewById(R.id.pb1).setVisibility(View.GONE);
        // Save the last visited URL
        saveUrl(url);
    }
};

public void saveUrl(String url) {
    SharedPreferences sp = getSharedPreferences("SP_WEBVIEW_PREFS",
            MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.putString("SAVED_URL", url);
    editor.commit();
}

public String getUrl() {
    SharedPreferences sp = getSharedPreferences("SP_WEBVIEW_PREFS",
            MODE_PRIVATE);
    // If you haven't saved the url before, the default value will be
    // google's page
    return sp.getString("SAVED_URL",
            "http://afreesms.com/worldwide/");
}

public void send(View view) { // send Button
    Toast toast = Toast.makeText(this, "Sending SMS..", Toast.LENGTH_SHORT);
    toast.show();
    webView.loadUrl("javascript:(function(){document.getElementById('submit').click();})()");
}

public void form(View view) { // form Button
    Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
    webView.loadUrl("http://www.afreesms.com/freesms/");
}

public void saveitbruh(View view) { // form Button
    saveUrl(null);
}

1 个答案:

答案 0 :(得分:0)

你应该使用下面给出的方法。

同时检查此link

 public void saveUrl(String url){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("SAVED_URL", url);
        editor.commit();
    }

    public String getUrl(){

      String url=  PreferenceManager.getDefaultSharedPreferences(this).getString("SAVED_URL", null);
//If you haven't saved the url before, the default value will be google's page
        return url;
    }