Android Webview Back按钮适用于Emualtor,但不适用于设备

时间:2017-12-30 04:16:18

标签: android button webview android-emulator back

我有以下JAVA Android Webview代码。后面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();
            }
        }
    }
}

由于

2 个答案:

答案 0 :(得分:1)

请在onBackPressed活动

上执行以下代码
if (webb.canGoBack()) {
   webb.goBack();
 } else {
   finish();
 }

答案 1 :(得分:0)

以下是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)