Android Webview Back按钮适用于模拟器,但不适用于设备

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Webview Back按钮适用于模拟器,但不适用于设备相关的知识,希望对你有一定的参考价值。

我有以下用于android JAVAWebview代码。后面的button在Android Studio模拟器中正常工作但是当我在任何Android设备上安装相同的apk时它不再起作用了。点击后面的button只会让移动设备上的应用程序崩溃。有人可以帮忙吗?

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

    public static final String PAGE_URL  = "www.domain.com";
    WebView webb;
    private static final int INPUT_FILE_REQUEST_CODE = 1;
    private static final int FILECHOOSER_RESULTCODE = 1;
    private static final String TAG = MainActivity.class.getSimpleName();
    private WebSettings webSettings;
    private ValueCallback<Uri> mUploadMessage;
    private Uri mCapturedImageURI = null;
    private ValueCallback<Uri[]> mFilePathCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webb = (WebView) findViewById(R.id.webb1);
        webSettings = webb.getSettings();
        webSettings.setjavascriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAllowFileAccess(true);
        //webb.setWebViewClient(new WebViewClient());
        webb.setWebViewClient(new Client());
        if (Build.VERSION.SDK_INT >= 19) {
            webb.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) {
            webb.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webb.loadUrl(PAGE_URL);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent     data) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
                super.onActivityResult(requestCode, resultCode, data);
                return;
            }
            Uri[] results = null;

            if (resultCode == Activity.RESULT_OK) {
                if (data == null) {
                    if (mCameraPhotoPath != null) {
                        results = new Uri[]{Uri.parse(mCameraPhotoPath)};
                    }
                } else {
                    String dataString = data.getDataString();
                    if (dataString != null) {
                        results = new Uri[]{Uri.parse(dataString)};
                    }
                }
            }
            mFilePathCallback.onReceiveValue(results);
            mFilePathCallback = null;
        } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
                super.onActivityResult(requestCode, resultCode, data);
                return;
            }
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (null == this.mUploadMessage) {
                    return;
                }
                Uri result = null;
                try {
                    if (resultCode != RESULT_OK) {
                        result = null;
                    } else {
                        result = data == null ? mCapturedImageURI : data.getData();
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "activity :" + e,
                            Toast.LENGTH_LONG).show();
                }
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
            }
        }
        return;
    }

    @Override
    public void onBackPressed() {
        webb = (WebView) findViewById(R.id.webb1);
        if (webb.canGoBack()) {
            webb.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webb != null && webb.canGoBack()) {
            webb.goBack();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public class Client extends WebViewClient {
        ProgressDialog progressDialog;

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("mailto:")) {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            } else if (url.contains("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                view.reload();
                return true;
            } else {
                view.loadUrl(url);
                return true;
            }
        }

        public void onPageFinished(WebView view, String url) {
            try {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }
}

谢谢

答案

请在onBackPressed活动上进行以下代码

if (webb.canGoBack()) {
   webb.goBack();
 } else {
   finish();
 }
另一答案

以下是android studio中的日志:

W/zygote: Attempt to remove non-JNI local reference, dumping thread
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
I/zygote: NativeAlloc concurrent copying GC freed 0(44KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2MB/4MB, paused 27us total 109.624ms
W/zygote: Attempt to remove non-JNI local reference, dumping thread
W/zygote: Attempt to remove non-JNI local reference, dumping thread
W/zygote: Attempt to remove non-JNI local reference, dumping thread
I/zygote: NativeAlloc concurrent copying GC freed 784(86KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1973KB/3MB, paused 21us total 113.240ms
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
W/zygote: Attempt to remove non-JNI local reference, dumping thread
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
I/zygote: NativeAlloc concurrent copying GC freed 3435(216KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1789KB/3MB, paused 52us total 118.789ms
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
I/zygote: Do full code cache collection, code=199KB, data=149KB
I/zygote: After code cache collection, code=197KB, data=114KB
W/zygote: Attempt to remove non-JNI local reference, dumping thread
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)
I/zygote: NativeAlloc concurrent copying GC freed 1118(111KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1844KB/3MB, paused 45us total 142.205ms
D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)

以上是关于Android Webview Back按钮适用于模拟器,但不适用于设备的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Espresso Kakao 测试中单击 webview 按钮

React Native - 如何在 React Native 应用程序中使用 WebView 显示我的网站通知(适用于 Android)

适用于所有移动设备的Webview App并上传到Play商店

Webview shouldOverrideUrlLoading 适用于除 4.1.x jellybean 之外的之前的 android 版本

我的 webview 视频适用于模拟器,但不适用于设备

检查文件是不是已经存在于webview缓存android中