我'我正在面对android webview中的两个问题。
请帮帮我。谢谢
public class MainActivity extends AppCompatActivity {
private WebView webView;
ProgressBar webpageprogressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
webpageprogressbar = (ProgressBar) findViewById(R.id.progressBar);
webpageprogressbar.setProgress(0);
webpageprogressbar.setMax(100);
MainActivity.this.webpageprogressbar.setProgress(0);
if(savedInstanceState !=null)
{
webView.restoreState(savedInstanceState);
}else {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
}
CookieSyncManager.createInstance(MainActivity.this);
CookieManager manager = new CookieManager();
Log.i("My APP","At onCreate");
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
final Activity activity = this;
webView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView web = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (web.canGoBack()) {
web.goBack();
return true;
}
break;
}
}
return false;
}
});
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("My APP","At shouldOverrideRlLoading");
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("My APP", "At onPageFinished");
CookieSyncManager.getInstance().sync();
super.onPageFinished(view, url);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
if(newProgress ==100)
{
webpageprogressbar.setVisibility(View.GONE);
}else {
webpageprogressbar.setVisibility(View.VISIBLE);
}
}
});
if (savedInstanceState == null) {
Log.d("My APP","At savedInstanceState");
webView.loadUrl("http://google.com");
}
if (Build.VERSION.SDK_INT >= 19) {
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
}
public void setValue(int progress) {
MainActivity.this.webpageprogressbar.setProgress(progress);
}
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
@Override
protected void onResume() {
Log.d("My APP","At onResume");
CookieSyncManager.getInstance().startSync();
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onPause() {
Log.d("My APP","At onPause");
CookieSyncManager.getInstance().stopSync();
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
清单文件: -
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:configChanges="orientation|screenSize"
android:theme="@style/AppTheme" >
答案 0 :(得分:0)
将下述标记添加到声明活动的清单文件中:
android:configChanges="keyboardHidden|orientation|screenSize"
例如:<activity
android:name="com.test.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape" />
存储Cookie:
// Create instance
CookieSyncManager syncManager = CookieSyncManager.createInstance(webView.getContext());
// Get instance
CookieManager cookieManager = CookieManager.getInstance();
// Set cookie
cookieManager.setCookie();
// Sync cookie
syncManager.sync();
随时可以询问任何查询。