Save Webview Cookies?

I am trying to get this WebView for Android code to store session cookies so that when you close the application and restart the users, they can remain in the system. I am very new to this and I get upset trying to figure it out. What do I need to change? Please be kind as an idiot and please describe, even if you need to edit it for me, if you find it easier.

I have imported both CookieManager and CookieSyncManager, but I have no IDEA what to do next. I read all the documentation and spent hours trying to figure out how to do this, save cookies so that users cannot log in again, and I just can't figure it out ...

package com.template.WebViewTemplate; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; public class WebViewTemplate extends Activity { private WebView myWebView; /** Called when the activity is first created. */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { // Enables browsing to previous pages with the hardware back button myWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myWebView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml myWebView.getSettings().setJavaScriptEnabled(true); myWebView.loadUrl("http://zuneboards.com/forums/mgc_chatbox.php?do=view_chatbox"); // Specify the URL to load when the application starts //myWebView.loadUrl("file://sdcard/"); // Specify a local file to load when the application starts. Will only load file types WebView supports myWebView.setWebViewClient(new WebViewKeep()); myWebView.setInitialScale(1); // Set the initial zoom scale myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control } private class WebViewKeep extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } 

}}

+4
source share
1 answer

You can use this with Websetting:

 webView.getSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
+5
source

Source: https://habr.com/ru/post/1382080/


All Articles