Android WebVIew模拟按钮点击并确认

时间:2016-11-08 22:53:32

标签: java android button webview

我是Android开发中的绝对新手,所以期待一些非常弱的代码,但这是迄今为止我所拥有的:

public class Mywebview2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final WebView webview = new WebView(this);
    setContentView(webview);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);

    webview.setWebViewClient(new MyWebViewClient());
    webview.loadUrl("http://192.168.1.1/ADV_home.htm");
    new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
        /*Context context = getApplicationContext();
        CharSequence text = "test1";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();*/
        }

        @Override
        public void onFinish() {
            Context context2 = getApplicationContext();
            CharSequence text2 = "test2";
            int duration2 = Toast.LENGTH_SHORT;

            Toast toast2 = Toast.makeText(context2, text2, duration2);
            toast2.show();
            webview.loadUrl("javascript:(function(){"+
                    "l=document.getElementByName('Neustart');"+
                    "e=document.createEvent('HTMLEvents');"+
                    "e.initEvent('click',true,true);"+
                    "l.dispatchEvent(e);"+
                    "})()");
        }

    }.start();

}
private class MyWebViewClient extends WebViewClient {
        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {

            handler.proceed("admin", "password");


        }

        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            result.confirm();
            return true;
        }
          };

        }

基本上我正在尝试创建一个登录我的路由器Web界面并执行重启的应用程序。 我设法成功登录,但现在我需要它来执行实际点击,然后确认警报,我在过去几个小时内无法这样做

按钮实现如下

<button type="button" class="page-table-button" style="float:center" onclick="advButtonClick(2);" onmouseover="changeCursorPointer();" onmouseout="changeCursorDefault();"><span id="reboot">Neustart</span></button>

并且警报窗口只是一个基本的确认警报,请看这里:

alert window

0 个答案:

没有答案