提示,警报和确认JavaScript在Android WebView中找不到

Prompt, Alert and confirm JavaScript inside a Android WebView not found

本文关键字:Android WebView 找不到 JavaScript 确认 提示      更新时间:2023-09-26

我正在android上编写一个应用程序,显示从Ip摄像机传输,这个camarea是一个D-Link,看到传输我去Ip: 192.168.1.4/MJPEG.CGI?.mjpeg,它在我的导航器中工作,但要访问它要求我通过Javascript提示为管理化合物提供一个帐户:输入图片描述

在我的android应用程序中,我使用的是带有片段的可绘制导航,我已经指定了一个片段用于传输,我使用的是WebView,我的片段的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.promindsoft.theprodigyeye.MainFragment">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Enter Text"
        android:focusable="true"
        android:textColorHighlight="#ff7eff15"
        android:textColorHint="#f23012"
        android:layout_marginTop="46dp"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/human_greeting"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter"
        android:id="@+id/button"
        android:layout_alignTop="@+id/editText"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />
    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

这很简单,我的按钮是加载url这是允许的到我的EditText,到我的mainfragment。java:

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {
    Button b1;
    EditText ed1;
    private WebView mWebView ;
    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        b1=(Button)view.findViewById(R.id.button);
        ed1=(EditText)view.findViewById(R.id.editText);
        ed1.setText("http://192.168.1.76/javas.html");
        mWebView =(WebView)view.findViewById(R.id.webView);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"Okey",Toast.LENGTH_SHORT).show();
                String url = ed1.getText().toString();
                WebSettings webSettings = mWebView.getSettings();
                webSettings.setPluginState(WebSettings.PluginState.ON);
                webSettings.setJavaScriptEnabled(true);
                webSettings.setUseWideViewPort(true);
                webSettings.setLoadWithOverviewMode(true);
                mWebView.loadUrl(url);
                mWebView.setWebViewClient(new MyBrowser());
            }
        });
        return view;
    }

    private class MyBrowser extends WebViewClient  {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.setWebChromeClient(new WebChromeClient() {
                private View mCustomView;
               @Override
                public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                   AlertDialog.Builder b = new AlertDialog.Builder(getContext())
                           .setTitle(view.getTitle())
                           .setMessage(message)
                           .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialog, int which) {
                                   result.confirm();
                               }
                           })
                           .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialog, int which) {
                                   result.cancel();
                               }
                           });
                   b.show();
                   // Indicate that we're handling this manually
                   return true;
               }
                @Override
                public boolean onJsAlert(WebView view, String url, String message, JsResult result)
                {
                    new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
                    result.confirm();
                    return true;
                }
                @Override
                public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
                {
                    // if a view already exists then immediately terminate the new one
                    if (mCustomView != null)
                    {
                        callback.onCustomViewHidden();
                        return;
                    }
                }

            });
            webview.loadUrl(url);
            return true;
        }

    }
}

我一直在用另一个Js文件做测试,试图得到警报或确认警报,但我不能得到它。

JS:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        //document.write("Shit")
            alert("Hola")

   var x;
    if (confirm("Press a button!") == true) {
        x = "You pressed OK!";
    } else {
        x = "You pressed Cancel!";
    }
    document.write(x);

     var person = prompt("Please enter your name");
    if (person != null) {
        document.write("Hello " + person + "! How are you today?");
    }
    </script>
</head>
<body>
<h1>Okey</h1>
</body>
</html>

对于具有警报决定的示例,我尝试使用onJsConfirm,但没有发生任何事情,并且较少使用给我传输的页面,曾经对登录采取了否定的答案:

输入图片描述

我真的需要帮助,我已经看了很多页面和视频来找到做它的方法,但我没有答案。有人能帮帮我吗?

设置mWebView的WebChromeClient只是流动它的初始化,NOT in method: shouldOverrideUrlLoading()

代码如下:

....
b1=(Button)view.findViewById(R.id.button);
ed1=(EditText)view.findViewById(R.id.editText);
ed1.setText("http://192.168.1.76/javas.html");
mWebView =(WebView)view.findViewById(R.id.webView);
// ***** PUT YOUR CODE HERE! *****
mWebView.setWebChromeClient(new WebChromeClient() {...}