调用使用JScript的Andr​​oid功能 [英] Calling Android Function using Jscript

查看:139
本文介绍了调用使用JScript的Andr​​oid功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序中,我一直在使用的Andr​​oid接口PHP页面...

i am creating a app in which i have to interface php page using android...

我在Android开发新手...我用 HTTP给出的code:// android- er.blogspot.in ..for演示使用相同的codeI创建一个小的应用程序,并试图在模拟器......但点击点击按钮,什么也没有发生之后......也logcat中没有显示任何错误。 ..

i am newbie in android dev... i use the code given in http://android-er.blogspot.in ..for demo using same code i create a small app and tried on emulator ...but after clicking on click button nothing is happening...also logcat is not displaying any error...

我的应用程序的内容这些文件...
1 ... JAVA code为mainactivity结果
2 .... HTML code(在Apache服务器保存)
3 ... main.xml中包含Web视图

my app content these files... 1...java code for mainactivity
2....html code (save in apache server) 3... main.xml contain web view

1 --- code主activty ..

1---code for main activty..

package com.example.prac_jscript;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView mybrowser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    mybrowser =(WebView)findViewById(R.id.webView1) ;

    mybrowser.addJavascriptInterface(new MyjavaScriptInterface(this), "AndroidFUNCTION");

     mybrowser.getSettings().setJavaScriptEnabled(true);

     mybrowser.loadUrl("http://192.168.1.3/copy.html");
    }


    public class MyjavaScriptInterface{


        Context mContext;
        public MyjavaScriptInterface(Context c) {
            // TODO Auto-generated constructor stub

             mContext = c;
        }

        public void openAndroidDialog(){
             AlertDialog.Builder myDialog
             = new AlertDialog.Builder(MainActivity.this);
             myDialog.setTitle("DANGER!");
             myDialog.setMessage("You can do what you want!");
             myDialog.setPositiveButton("ON", null);
             myDialog.show();
            }
    }

}

2 - $ HTML文件的C $ç

2 - code of html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>
    <label>
    <input type="submit" value="Copy" onClick="openAndroidDialog()"/>
<script type="text/javascript">
 function openAndroidDialog() {
     AndroidFunction.openAndroidDialog();
 }
</script>

    </label>
  </p>

</body>
</html>

请帮我...

推荐答案

不用担心小失误

改变这一行

mybrowser.addJavascriptInterface(new MyjavaScriptInterface(), "AndroidFUNCTION");

mybrowser.addJavascriptInterface(new MyjavaScriptInterface(), "AndroidFunction");

更新:

JavascriptInterface 想这个

UPDATE:

Your JavascriptInterface Should like this

 public class MyjavaScriptInterface{



        MyjavaScriptInterface() {

        }

        @JavascriptInterface
        public void openAndroidDialog(){
            AlertDialog.Builder myDialog
                    = new AlertDialog.Builder(MainActivity.this);
            myDialog.setTitle("DANGER!");
            myDialog.setMessage("You can do what you want!");
            myDialog.setPositiveButton("ON", null);
            myDialog.show();
        }
    }

OUTPUT:

如何打开watsapp?

Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.setType("text/plain");
            String text = "YOUR TEXT HERE";
    waIntent.setPackage("com.whatsapp");
    if (waIntent != null) {
        waIntent.putExtra(Intent.EXTRA_TEXT, text);//
        startActivity(Intent.createChooser(waIntent, "Share with"));
    } else {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
    }

这篇关于调用使用JScript的Andr​​oid功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆