zoukankan      html  css  js  c++  java
  • Android JS interaction

    WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    web=(WebView) findViewById(R.id.web);
    web.getSettings().setDefaultTextEncodingName("UTF-8") ;
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setAllowFileAccess(true);
    web.addJavascriptInterface(new JsOperator(MainActivity.this),"JsInteraction");
    web.loadData("test<script type="text/javascript">function showMsg(){JsInteraction.showDialog("Login start...");}", "text/html", "UTF-8");
    //shareMsg("active ttt","ttttttt","test http://www.qq.com/ 测试","/assets/1.jpg");

    }

    package com.wgscd.gwang.myapplication;

    /**

    • Created by gwang on 2017/3/14.
      */

    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.webkit.JavascriptInterface;
    import org.json.JSONObject;

    public class JsOperator {

    private Context context;
    
    public JsOperator(Context context) {
        this.context = context;
    }
    
    /**
     * 弹出消息对话框
     */
    @JavascriptInterface
    public void showDialog(String message) {
    
        AlertDialog.Builder builder = new Builder(context);
        builder.setMessage(message);
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
    
    /**
     * 获取登录的用户名和密码
     * @return JSON格式的字符串
     */
    @JavascriptInterface
    public String getLoginInfo(){
        try{
            JSONObject login = new JSONObject();
            login.put("Username", "YLD");
            login.put("Password", "111");
    
            return login.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
    
        return null;
    }
    

    }

  • 相关阅读:
    12.C语言控制窗口
    11.字符,字符常见开发,_itoa函数
    Clusterware 和 RAC 中的域名解析的配置校验和检查 (文档 ID 1945838.1)
    导致实例逐出的五大问题 (文档 ID 1526186.1)
    如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)
    11gR2新特性---Gpnp守护进程
    CSS 功能简介
    11gR2新特性---gipc守护进程
    10g集群启动顺序
    11gR2集群件任务角色分离(Job Role Separation)简介
  • 原文地址:https://www.cnblogs.com/wgscd/p/6635773.html
Copyright © 2011-2022 走看看