本文目錄一覽:
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()”);
}
});
}
});
}
}
nodejs 在 android 系統裡面怎麼運行
每種語言都有自己的優勢,互相結合起來各取所長程序執行起來效率更高或者說哪種實現方式較簡單就用哪個,nodejs是利用子進程來調用系統命令或者文件,文檔見nodejs.org/api/child_process.html,NodeJS子進程提供了與系統交互的重要介面,其主要API有:標準輸入、標準輸出及標準錯誤輸出的介面。NodeJS子進程提供了與系統交互的重要介面,其主要API有:標準輸入、標準輸出及標準錯誤輸出的介面child.stdin獲取標準輸入child.stdout獲取標準輸出child.stderr獲取標準錯誤輸出獲取子進程的PID:child.pid提供生成子進程的方法:child_process.spawn(cmd,args=[],[options])提供直接執行系統命令的方法:child_process.exec(cmd,[options],callback)提供調用腳本文件的方法:child_process.execFile(file,[args],[options],[callback])提供殺死進程的方法:child.kill(signal=’SIGTERM’)用實例來感受一下,很有意思的,呵呵~~1、利用子進程調用系統命令(獲取系統內存使用情況)新建nodejs文件,名為cmd_spawn.js,代碼如下:複製代碼代碼如下:varspawn=require(‘child_process’).spawn;free=spawn(‘free’,[‘-m’]);//捕獲標準輸出並將其列印到控制台free.stdout.on(‘data’,function(data){console.log(‘standardoutput:\n’+data);});//捕獲標準錯誤輸出並將其列印到控制台free.stderr.on(‘data’,function(data){console.log(‘standarderroroutput:\n’+data);});//註冊子進程關閉事件free.on(‘exit’,function(code,signal){console.log(‘childprocesseixt,exit:’+code);});
android 中如何去執行js腳本
按照給定的字符集存儲文件時,在文件的最開頭的三個位元組中就有可能存儲著編碼信息,所以,基本的原理就是只要讀出文件前三個位元組,判定這些位元組的值,就可以得知其編碼的格式。
其實,如果項目運行的平台就是中文操作系統,
如果這些文本文件在項目內產生,即開發人員可以控制文本的編碼格式,
只要判定兩種常見的編碼就可以了:gbk和utf-8。
由於中文windows默認的編碼是gbk,所以一般只要判定utf-8編碼格式。
Android調用js的問題
Android中可以使用WebView載入網頁,同時Android端的java代碼可以與網頁上的javascript代碼之間相互調用。
一 Android部分:
布局代碼:
LinearLayout xmlns:android=”” xmlns:tools=”” android:layout_width=”match_parent” android:layout_height=”match_parent” android:focusable=”true” android:focusableInTouchMode=”true” android:orientation=”vertical” android:padding=”8dp” tools:context=”.MainActivity” LinearLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” EditText android:id=”@+id/input_et” android:layout_width=”0dp” android:layout_height=”wrap_content” android:singleLine=”true” android:layout_weight=”1″ android:hint=”請輸入信息” / Button android:text=”Java調用JS” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:onClick=”sendInfoToJs” / /LinearLayout WebView android:id=”@+id/webView” android:layout_width=”match_parent” android:layout_height=”match_parent” / /LinearLayout
Activity代碼:
public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView); webView.setVerticalScrollbarOverlay(true); //設置WebView支持JavaScript webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(“”); //在js中調用本地java方法 webView.addJavascriptInterface(new JsInterface(this), “AndroidWebView”); // //添加客戶端支持 webView.setWebChromeClient(new WebChromeClient()); } private class JsInterface { private Context mContext; public JsInterface(Context context) { this.mContext = context; } //在js中調用window.AndroidWebView.showInfoFromJs(name),便會觸發此方法。 @JavascriptInterface public void showInfoFromJs(String name) { Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show(); } } //在java中調用js代碼 public void sendInfoToJs(View view) { String msg = ((EditText) findViewById(R.id.input_et)).getText().toString(); //調用js中的函數:showInfoFromJava(msg) webView.loadUrl(“javascript:showInfoFromJava(‘” + msg + “‘)”); // webView.loadUrl(“javascript:showInfoFromJava()”); } }
二 網頁代碼
!DOCTYPE html html lang=”en” head meta charset=”UTF-8″ titleAndroid WebView 與 Javascript 交互/title /head body input type=”button” value=”分享” onclick=”f1()” input type=”text” id=”show”/ /body script function f1(){ AndroidWebView.showInfoFromJs(“hello”); } function showInfoFromJava(msg){ document.getElementById(“show”).value=msg; alert(1); } /script /html
注意: android 調用js代碼可能會報錯如下:
W/WebView(2088): java.lang.Throwable: A WebView method was called on thread ‘JavaBridge’. All WebView methods must be called on the same thread.
解決辦法:
webView.post(new Runnable() { @Override public void run() { webView.loadUrl(“javascript:showInfoFromJava(‘” + msg + “‘)”); }});
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/159186.html