我的网站需要登录才能使用,是否可以为用户保存登录数据,这样他们每次进入WebView(例如Firebase或COOKIEs等)时都不会登录?
如果您需要共享我的代码,请告诉我.
谢谢.
编辑1:
这是MainActivity.java文件代码:
package com.company.app;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.COOKIEManager;
import android.webkit.COOKIESyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.webView).setVisibility(View.GONE);
swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
WebAction();
}
});
COOKIESyncManager.createInstance(this);
COOKIEManager COOKIEManager = COOKIEManager.getInstance();
COOKIEManager.setAcceptCOOKIE(true);
WebView webview = new WebView(this);
WebSettings ws = webview.getSettings();
ws.setSaveFormData(true);
ws.setSavePassword(true); // Not needed for API level 18 or greater (deprecated)
WebAction();
}
public void WebAction(){
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavascriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// chromium, enable hardware acceleration
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
String myURL = "https://www.myURL.com/";
COOKIESyncManager.createInstance(this);
COOKIEManager COOKIEManager = COOKIEManager.getInstance();
COOKIEManager.setAcceptCOOKIE(true);
WebView webview = new WebView(this);
WebSettings ws = webview.getSettings();
ws.setSaveFormData(true);
ws.setSavePassword(true); // Not needed for API level 18 or greater (deprecated)
webView.loadUrl(myURL);
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
// do your stuff here
swipe.setRefreshing(false);
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
});
}
@Override
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}else {
finish();
}
}
}