android調用js中的值,js開發android應用

本文目錄一覽:

android怎樣調用js文件裡面的方法

android怎樣調用js文件裡面的方法

如果多個線程同時訪問一個集合,而其中至少一個線程修改了該集合,那麼它必須 保持外部同步。這通常是通過對自然封裝該集合的對象執行同步操作來完成的。如果不存在這樣的對象,則應該使用 Collections.synchronizedSet 方法來「包裝」集合。最好在創建時完成這一操作,以防止對 HashSet 實例進行意外的不同步訪問:

Set s = Collections.synchronizedSet(new HashSet(…));

android 怎麼調用js方法

思路:

1、需要使用webview打開網頁

2、設置webview支持腳本

3、然後通過webview的loadUrl方式進行js函數調用

android如何調用js文件裡面的方法並獲得返回指呢

直接調用就可以啊,是js要調用app上的,才需要在app寫上註冊腳本的代碼

在android中怎樣調用本地js文件里的方法並得到返回值

在android中調用本地js文件里的方法並得到返回值其方法如下:

Android中內置了WebKit模塊,而該模塊的Java層視圖類就是WebView,所有需要使用Web瀏覽器功能的Android都需要創建該視圖類對象顯示和處理請求的網路資源。目前WebKit支持Http、Https、Ftp和JavaScript請求。下面是在Android中調用JavaScript方法以及如何在js中調用本地方法。

1、在Assets下放一個簡單的html文件jstest.html

!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “”

HTML

HEAD

meta name=”viewport” content=”width=device-width, target-densitydpi=device-dpi” /

META http-equiv=”Content-Type” content=”text/html; charset=UTF-8″

script

function showMsg(){

alert(“hello world!”);

}

function showMsgInAndroid(){

myjs.showMsg(‘hello in android!’);

}

/script

/HEAD

BODY

span測試js使用/span

button id=’btntest’ onclick=’showMsgInAndroid()’調用android方法/button

/BODY

/HTML

android里如何調用Js里的函數

Android中內置了WebKit模塊,而該模塊的Java層視圖類就是WebView,所有需要使用Web瀏覽器功能的Android都需要創建該視圖類對象顯示和處理請求的網路資源。目前WebKit支持Http、Https、Ftp和JavaScript請求。

1、在Assets下放一個簡單的html文件jstest.html

HTML

HEAD

meta name=”viewport” content=”width=device-width, target-densitydpi=device-dpi” /

META http-equiv=”Content-Type” content=”text/html; charset=UTF-8″

script

   function showMsg(){

      alert(“hello world!”);

   }

   function showMsgInAndroid(){

      myjs.showMsg(‘hello in android!’);

   }

/script

/HEAD

BODY

span測試js使用/span

button id=’btntest’ onclick=’showMsgInAndroid()’調用android方法/button

/BODY

/HTML

2、布局文件main.xml

?xml version=”1.0″ encoding=”utf-8″?

RelativeLayout

android:id=”@+id/rl_main”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

xmlns:android=””

    

   WebView 

       android:id=”@+id/wv_test”

       android:layout_width=”fill_parent”

       android:layout_height=”fill_parent”

       android:layout_above=”@+id/btn_showmsg”/

   Button 

       android:id=”@+id/btn_showmsg”

       android:layout_width=”200dip”

       android:layout_height=”40dip”

       android:layout_alignParentBottom=”true”

       android:layout_centerHorizontal=”true”

       android:text=”調用html中js方法”/

/RelativeLayout

3、然後是Activity,MainActivity.java

package com.harold.jstest;

import com.harold.base.JSKit;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.widget.Button;

public class MainActivity extends Activity {

private WebView mWebView;

private Button btnShowInfo;

private JSKit js;

private Handler mHandler = new Handler();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//初始化控制項

mWebView = (WebView) findViewById(R.id.wv_test);

btnShowInfo = (Button) findViewById(R.id.btn_showmsg);

//實例化js對象

js = new JSKit(this);

//設置參數

mWebView.getSettings().setBuiltInZoomControls(true);

//內容的渲染需要webviewChromClient去實現,

//設置webviewChromClient基類,解決js中alert不彈出的問題和其他內容渲染問題

mWebView.setWebChromeClient(new WebChromeClient());

mWebView.getSettings().setJavaScriptEnabled(true);

//把js綁定到全局的myjs上,myjs的作用域是全局的,初始化後可隨處使用

mWebView.addJavascriptInterface(js, “myjs”);

mWebView.loadUrl(“”);

btnShowInfo.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

mHandler.post(new Runnable() {

@Override

public void run() {

//調用 HTML 中的javaScript 函數

mWebView.loadUrl(“javascript:showMsg()”);

}

});

}

});

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193636.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-01 15:02
下一篇 2024-12-01 15:02

相關推薦

發表回復

登錄後才能評論