Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载Android时如何调用javascript方法_Javascript_Android_Hybrid Mobile App - Fatal编程技术网

加载Android时如何调用javascript方法

加载Android时如何调用javascript方法,javascript,android,hybrid-mobile-app,Javascript,Android,Hybrid Mobile App,我是android的初学者 我在android中使用了javascript方法。我想在第一次加载应用程序时调用它 比如说, testMethod = function(str) { alert("hi" + str); }//javascript 及 我在这里想的是,当我启动应用程序时,警报会立即弹出 但什么也没发生 关于这一点,我有两个问题 1.我可以在Android的onCreate方法中调用并使用javascript方法吗 2.如果可能,我该怎么办?如果您想在启动时显示弹出窗口,只需

我是android的初学者

我在android中使用了javascript方法。我想在第一次加载应用程序时调用它

比如说,

testMethod = function(str) {
  alert("hi" + str);
}//javascript

我在这里想的是,当我启动应用程序时,警报会立即弹出

但什么也没发生

关于这一点,我有两个问题

1.我可以在Android的onCreate方法中调用并使用javascript方法吗


2.如果可能,我该怎么办?

如果您想在启动时显示弹出窗口,只需使用默认的AlertDialog。请参阅下面的代码

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showMessage("My Message Here", "Title here");
}

private void showMessage(String data, String title){
        new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(data)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue
                    }
                })
                .show();
    }
  • 检查index.html是否真的加载了。我猜webview没有正确加载html,因为它不是无效的文件路径。尝试file:///mnt/sdcard/index.html. (或有可能的事)
  • 尝试将
    mWebView.loadUrl(“javascript:testMethod('javascript')”)
    放在view.post或view.postDelayed中

  • 谢谢你的回答!这很好,但我真正想做的是获取javascript的值并将其打印出来。最后,我将在android中使用该值。我可以用你的答案来做这件事吗?html页面加载得很好,但仍然不起作用。我认为,这可能是javascript错误。谢谢你的帮助!
    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showMessage("My Message Here", "Title here");
    }
    
    private void showMessage(String data, String title){
            new AlertDialog.Builder(this)
                    .setTitle(title)
                    .setMessage(data)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // continue
                        }
                    })
                    .show();
        }