www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 嵌入式 > 嵌入式軟件
[導(dǎo)讀] 在Android中通過WebView控件,可以實(shí)現(xiàn)要加載的頁面與Android方法相互調(diào)用,我們要實(shí)現(xiàn)WebView中的addJavascriptInterface方法,這樣html才能調(diào)用android方法。為了讓大家容

 在Android中通過WebView控件,可以實(shí)現(xiàn)要加載的頁面與Android方法相互調(diào)用,我們要實(shí)現(xiàn)WebView中的addJavascriptInterface方法,這樣html才能調(diào)用android方法。

為了讓大家容易理解,寫了一個(gè)簡單的Demo,具體步驟如下:

第一步:新建一個(gè)Android工程,命名為WebViewDemo(這里我在assets里定義了一個(gè)html頁面)。

第二步:修改main.xml布局文件,增加了一個(gè)WebView控件還有Button控件,代碼如下:

view plaincopy to clipboardprint?

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Welcome to Mr Wei‘s Blog."

/>

android:id="@+id/webview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Change the webview content"

/>

第三步:在assets目錄下新建一個(gè)demo.html文件,代碼如下(這里不知道為何多了mce:這幾個(gè)東東,這樣是對的):

view plaincopy to clipboardprint?

Start GoogleMap

A Demo ----Android and Javascript invoke each other.

Author:Frankiewei

第四步:修改主核心程序WebViewDemo.java,代碼如下:

view plaincopy to clipboardprint?

package com.tutor.webwiewdemo;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.widget.Button;

public class WebViewDemo extends Activity {

private WebView mWebView;

private Button mButton;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setupViews();

}

//初始化

private void setupViews() {

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

WebSettings mWebSettings = mWebView.getSettings();

//加上這句話才能使用javascript方法

mWebSettings.setJavaScriptEnabled(true);

//增加接口方法,讓html頁面調(diào)用

mWebView.addJavascriptInterface(new Object() {

//這里我定義了一個(gè)打開地圖應(yīng)用的方法

public void startMap() {

Intent mIntent = new Intent();

ComponentName component = new ComponentName(

"com.google.android.apps.maps",

"com.google.android.maps.MapsActivity");

mIntent.setComponent(component);

startActivity(mIntent);

}

}, "demo");

//加載頁面

mWebView.loadUrl("file:///android_asset/demo.html");

mButton = (Button) findViewById(R.id.button);

//給button添加事件響應(yīng),執(zhí)行JavaScript的fillContent()方法

mButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

mWebView.loadUrl("javascript:fillContent()");

}

});

}

}

第五步:運(yùn)行上述工程,查看效果。

首界面

點(diǎn)擊按鈕時(shí),html內(nèi)容改變

點(diǎn)擊html的startGoogleMap啟動地圖應(yīng)用

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

其他電腦(比如安卓手機(jī)/平板電腦)的屏幕壞了,你可能想在安排維修之前緊急訪問一些東西。你可以使用android的USB OTG功能(是的,幾乎每個(gè)android都支持這個(gè)功能,你可以將鼠標(biāo)和鍵盤連接到它)。

關(guān)鍵字: USB 鼠標(biāo) Android 樹莓派

Google 宣布與中國 AR 科技公司 XREAL 達(dá)成深度戰(zhàn)略合作,聯(lián)合推出全球首款專為 Android XR 平臺打造的旗艦級 AR 眼鏡 Project Aura。

關(guān)鍵字: Google XREAL Android XR眼鏡 AR

繼停止維護(hù)AOSP開源項(xiàng)目后,谷歌母公司Alphabet近日被曝在其安卓系統(tǒng)(Android)、Pixel手機(jī)以及Chrome瀏覽器等部門裁員數(shù)百人。這一舉措引發(fā)了業(yè)界的廣泛關(guān)注,也引發(fā)了對谷歌未來業(yè)務(wù)布局的諸多猜測。

關(guān)鍵字: 谷歌 AOSP Android 裁員

在本教程中,我們將使用Capacitor 6、Angular和TypeScript構(gòu)建一個(gè)Android應(yīng)用程序,該應(yīng)用程序通過串行端口連接到BleuIO USB加密狗。該應(yīng)用程序允許用戶直接從Android設(shè)備發(fā)送和接...

關(guān)鍵字: Android USB 電容器 BLE設(shè)備

早前媒體報(bào)道谷歌將停止維護(hù)Android開源項(xiàng)目(AOSP),將Android開發(fā)全面轉(zhuǎn)向內(nèi)部閉源分支,目前這一消息已經(jīng)得到谷歌官方確認(rèn)。

關(guān)鍵字: 谷歌 Android 開源

本項(xiàng)目演示了如何通過OTG (on - go) USB在Android設(shè)備上使用BleuIO USB加密狗作為串行端口。使用電容器6和@adeunis/電容器-串行插件,我們建立串行連接,發(fā)送AT命令,并實(shí)時(shí)讀取響應(yīng)。該...

關(guān)鍵字: 電容器 Android 傳感器 微控制器 嵌入式系統(tǒng)

在Linux操作系統(tǒng)中,Android Debug Bridge(ADB)是一個(gè)功能強(qiáng)大的命令行工具,它允許開發(fā)者在計(jì)算機(jī)和Android設(shè)備之間建立通信,從而進(jìn)行調(diào)試、管理、安裝應(yīng)用等操作。本文將詳細(xì)介紹在Linux系...

關(guān)鍵字: Linux系統(tǒng) Android Debug ADB

隨著Android操作系統(tǒng)的進(jìn)步,智能手機(jī)的使用日益增加。隨后,有報(bào)道稱,惡意個(gè)人和黑客利用 Android 提供的漏洞來訪問用戶珍視的數(shù)據(jù)。例如,此類威脅包括 2021 年針對 Android 設(shè)備發(fā)布的 Flubot...

關(guān)鍵字: Android 惡意軟件

在本教程中,我們將構(gòu)建超出電子領(lǐng)域的東西。作為一名電子工程師,我們大多數(shù)人都想為我們的物聯(lián)網(wǎng)應(yīng)用程序構(gòu)建一些用戶界面,在大多數(shù)情況下,Android應(yīng)用程序?qū)⑹怯脩襞c我們的物聯(lián)網(wǎng)設(shè)備交互的正確選擇。所以,如果你想為你的物...

關(guān)鍵字: 物聯(lián)網(wǎng) Android
關(guān)閉