简体   繁体   English

Android 4.x删除我的Cookie

[英]Android 4.x Deletes My Cookies

I have an WebView Android application, that loads an local html file. 我有一个WebView Android应用程序,可加载本地html文件。 In this file, I create a cookie using javascript. 在此文件中,我使用javascript创建一个cookie。 The application runs fine in Android 2.x. 该应用程序可以在Android 2.x中正常运行。 But in Android 4.x, the application doesn't save the cookies when I close and reopen the application. 但是在Android 4.x中,当我关闭并重新打开应用程序时,该应用程序不会保存cookie。

I searched in similar questions, but I haven't fixed the problem. 我搜索了类似的问题,但尚未解决问题。 Can you help me? 你能帮助我吗?

My java source: 我的java源代码:

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. 对于API级别为12以上的android,您需要确保cookie对于文件系统uri是可接受的。

There is a method in CookieManager. CookieManager中有一个方法。 You need to set that to true. 您需要将其设置为true。

CookieManager.setAcceptFileSchemeCookies(true); CookieManager.setAcceptFileSchemeCookies(true);

you can see more detail here.. 您可以在此处查看更多详细信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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