两年来我一直在寻找错误
当应用程序启动时,没有打开不同视图和执行某些操作的问题。但是当应用程序在后台运行一段时间(在10分钟到2-3小时之间)并且在恢复应用程序之后,用户界面被冻结并且没有任何响应也没有在logcat中打印错误。我们必须终止应用程序并重新启动才能使其正常工作。
我将代码最小化,以提供我的问题的最小代码示例
主要活动:
package com.example.test;
import com.example.test.HTML5WebView;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
HTML5WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = new HTML5WebView(this);
setContentView(myWebView.getLayout());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://xxxxx");
}
}
HTML5WebView:
package com.example.test;
import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class HTML5WebView extends WebView {
private Context mContext;
private MyWebChromeClient mWebChromeClient;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private FrameLayout mContentView;
private FrameLayout mBrowserFrameLayout;
private FrameLayout mLayout;
public ProgressDialog progress;
public String last_known_url = null;
static final String LOGTAG = "HTML5WebView";
private void init(Context context) {
mContext = context;
Activity a = (Activity) mContext;
mLayout = new FrameLayout(context);
mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custom_screen, null);
mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);
mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
// Configure the webview
WebSettings s = getSettings();
s.setBuiltInZoomControls(true);
s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
s.setUseWideViewPort(true);
s.setLoadWithOverviewMode(true);
// s.setSavePassword(true);
s.setSaveFormData(true);
s.setJavaScriptEnabled(true);
s.setDomStorageEnabled(true);
s.setCacheMode(WebSettings.LOAD_NO_CACHE);
s.setDatabaseEnabled(true);
mWebChromeClient = new MyWebChromeClient();
setWebChromeClient(mWebChromeClient);
setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
progressloading_hide();
System.out.println("onPageFinished url: " +url);
// Facebook redirects to this url once a user has logged in, this is a blank page so we override this
// http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php")){
String redirectUrl = "xxx";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
};
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
//System.out.println("ERKANNT: shouldOverrideUrlLoading"+url.toLowerCase());
if(url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https") || url.toLowerCase().startsWith("file"))
{
// System.out.println("shouldOverrideUrlLoading=FALSE STANDARD HANDLING");
return(false);
}
else
{
// System.out.println("shouldOverrideUrlLoading=URL SCHEMA CALL?");
try
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
view.getContext().startActivity(intent);
return (true);
}
catch (Exception e)
{
// System.out.println("shouldOverrideUrlLoading ERROR "+e.getMessage());
return (true);
}
}
}
}
});
// enable navigator.geolocation
// s.setGeolocationEnabled(true);
// s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");
// enable Web Storage: localStorage, sessionStorage
s.setDomStorageEnabled(true);
mContentView.addView(this);
}
public void progressloading_show(Context context) {
if (progress==null)
progress = new ProgressDialog(context);
progress.setTitle("Bitte warten");
progress.setMessage("Lade Daten...");
progress.show();
// To dismiss the dialog
//progress.dismiss();
}
public void progressloading_hide() {
if (progress!=null){
if (progress.isShowing())
progress.dismiss();
}
}
public HTML5WebView(Context context) {
super(context);
init(context);
}
public HTML5WebView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public HTML5WebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public FrameLayout getLayout() {
return mLayout;
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
System.out.println("System.out.onHideCustomView");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((mCustomView == null) && canGoBack()){
goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyWebChromeClient extends WebChromeClient {
private View mVideoProgressView;
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
//System.out.println("customview show");
HTML5WebView.this.setVisibility(View.GONE);
System.out.println("System.out.HTML5WebView GONE");
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
System.out.println("System.out.mCustomViewContainer VISIBLE");
}
@Override
public void onHideCustomView() {
//System.out.println("customview hideeeeeeeeeeeeeeeeeeeeeeeeeee");
if (mCustomView == null)
return;
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
System.out.println("System.out.mCustomView GONE");
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// if (canGoForward())
// HTML5WebView.this.goForward();
HTML5WebView.this.setVisibility(View.VISIBLE);
System.out.println("System.out.HTML5WebView VISIBLE");
// HTML5WebView.this.goBack();
//Log.i(LOGTAG, "set it to webVew");
}
@Override
public View getVideoLoadingProgressView() {
//Log.i(LOGTAG, "here in on getVideoLoadingPregressView");
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);
}
return mVideoProgressView;
}
@Override
public void onReceivedTitle(WebView view, String title) {
((Activity) mContext).setTitle(title);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
((Activity) mContext).getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}