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;
    }
    

    }

  • 相关阅读:
    jmeter 安装
    Day05_系统监控、rpm、yum软件包管理及源码安装python解释器
    Day04_vim编辑器及硬盘操作
    Day03_用户群组权限及正文处理命令
    Day02_操作系统、网络及Linux基础
    Day01_计算机硬件及启动流程
    让Sublime Text成为静态WEB服务器:SublimeServer
    sublime text2-text3 定义的不同浏览器的预览快捷键
    css之px自动转rem—“懒人”必备
    修改Sublime Text3 的侧边栏字体大小
  • 原文地址:https://www.cnblogs.com/wgscd/p/6635773.html
Copyright © 2011-2022 走看看