简体   繁体   中英

Android 4.x Deletes My Cookies

I have an WebView Android application, that loads an local html file. In this file, I create a cookie using javascript. The application runs fine in Android 2.x. But in Android 4.x, the application doesn't save the cookies when I close and reopen the application.

I searched in similar questions, but I haven't fixed the problem. Can you help me?

My java source:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        myWebView.getSettings().setAppCacheEnabled(true);
        myWebView.getSettings().setDatabaseEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        CookieManager cookieManager = CookieManager.getInstance(); 
        cookieManager.setAcceptCookie(true);
        CookieManager.getInstance().setAcceptCookie(true);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();

        myWebView.setWebViewClient(new WebViewClient());
        setContentView(myWebView); 
        myWebView.loadUrl("file:///android_asset/www/index.htm");
    }

For android with API level 12 and above you need to make sure that cookies is acceptable for file system uri.

There is a method in CookieManager. You need to set that to true.

CookieManager.setAcceptFileSchemeCookies(true);

you can see more detail here..

http://developer.android.com/reference/android/webkit/CookieManager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM