如何在Android Studio的WebView上以编程方式接受cookie?

问题描述 投票:0回答:1

我已经制作了一个Web应用程序,并希望以编程方式允许cookie,因此该网站上的“允许cookie”消息不会在我的应用程序中弹出。

到目前为止,我有这个:

public class MainActivity extends AppCompatActivity {


WebView webview;
ImageView Logo, FondBlanc;
ProgressBar progressBar;


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

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this,R.style.DialogStyle);
    alertDialogBuilder.setTitle("Unable to connect");
    alertDialogBuilder.setMessage("Please check your Internet connection");
    alertDialogBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String url = webview.getUrl();
            webview.loadUrl(url);
            Error = "";
        }
    });
    alertDialogBuilder.setCancelable(false);
    final AlertDialog alertDialog = alertDialogBuilder.create();

    Logo = findViewById(R.id.logo);
    FondBlanc = findViewById(R.id.fondBlanc);
    progressBar = findViewById(R.id.progressbar);
    progressBar.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);

    Error = "";

    webview = findViewById(R.id.webView);
    webview.setWebViewClient(new WebViewClient(){

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            String host = Uri.parse(url).getHost();
            String domain = "mywebsite.com";

            if (host.equals(domain)) {
                // This is my website, so do not override; let my WebView load the page
                return false;
            } else {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
        }

        @Override public void onReceivedError(WebView view, WebResourceRequest request,
                                              WebResourceError error) {
            super.onReceivedError(view, request, error);

            webview.stopLoading();

            Error = error.toString();

            alertDialog.show();
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(18);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#FFFFFF"));
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundResource(R.drawable.dialog_style);

        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {

            handler.proceed("username", "password");

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (Error.equals("")) {
                progressBar.setVisibility(View.GONE);
                Logo.setImageResource(0);
                FondBlanc.setImageResource(0);
            }
        }
    });


    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true);
        CookieManager.getInstance().acceptThirdPartyCookies(webview);
    } else {
        CookieManager.getInstance().setAcceptCookie(true);
    }
    CookieManager.setAcceptFileSchemeCookies(true);
    webview.loadUrl("mywebsite.com");

}
}

但是我仍然收到'allow cookies'消息:cookies

我想在不手动接受的情况下删除此消息,请问该如何做?我尝试使用在CookiesManager中发现的其他方法,但是没有一个起作用。

谢谢您的帮助。

android android-studio cookies webview android-webview
1个回答
0
投票

几个小时后我就知道了:

CookieSyncManager.createInstance(webview.getContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();

    cookieManager.setCookie(domain,"cookies-state=accepted");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
© www.soinside.com 2019 - 2024. All rights reserved.