是的,你可以通過JavaScript接口來實現。通過這個JavaScript可以調用您附加的界面中定義的函數。
http://developer.android.com/guide/webapps/webview.html
在你Activity
:
WebAppInterface mInterface;
public void onCreate() {
mInterface = new WebAppInterface(this);
mWebView.addJavascriptInterface(mInterface, "MyAndroidInterface")
}
現在定義的對象,其中的來電會轉到:
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
現在Javascript
你可以這樣做:
MyAndroidInterface.showToast("hello world");
回來的路上(從native
到Javascript
)你可以通過Javascript injection
。 在Javascript
定義功能:
function helloBack(param) {
}
現在,您可以撥打mWebView.loadUrl("javascript:helloBack(hi);");
優秀。只是我在找什麼。有了這個我可以發送Intents,訪問服務等使用這個接口? – jonney 2013-05-01 14:43:14
是的,請確保您的組件具有意圖發送所需的上下文。 – RvdK 2013-05-01 14:53:47