WebView 聯絡(要麼button)至 Activity 跳躍在幾個方面

來源:互聯網
上載者:User

標籤:

第一 ,寫一個 JavaScriptinterface 分類。內實現WebView至Activity 頁面跳轉

public class JavaScriptinterface {Activity mActivity;public JavaScriptinterface(Activity mActivity) {this.mActivity = mActivity;}/** 與js互動時用到的方法,在js裡直接調用的 */public void startActivity() {Intent intent = new Intent();intent.putExtra("fromWhich", "webViewUrl");intent.putExtra("replyID", "replyID");intent.setClass(mActivity, Testactvity.class);mActivity.startActivity(intent);}}

而webView 實現 myWebView.addJavascriptInterface(new JavaScriptinterface(this), "android");  加入 這個類,表情為 android。在對應的網頁中連結或button加入對應JS 

function startActivity() {       android.startActivity();    }
 

JavaScriptinterface  的 startActivity  與 JS中的方法必須一致

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><script type="text/javascript">   function startActivity() {       android.startActivity();    }</script></head><body><a href="#" id="a1" name="a1" onClick="startActivity()">button</a><input type="button" value="Say hello" onClick="startActivity()"/> </body></html>

完整Demo下載: http://download.csdn.net/download/jia635/8766869

方式二:

 通過在 Activity 中 的  <data android:scheme="topicid" />  scheme 去實現。同一時候這樣的方式可對外開發。即別的APP中遵循這個 scheme 規則,就可以開啟注冊scheme 的Activity

        <activity
            android:name="com.example.webviewtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />


                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="topicid" />
            </intent-filter>
        </activity>

      在WebView 的主要代碼例如以下 <a href="topicid://aa.bb:80/test?

p=12&d=1" id="a1" name="a1"  >button</a>    超串連和scheme 一直 ,然後通過 Intent 傳值能夠通過  topicid://aa.bb:80/test?

p=12&d=1 欄位去截取想要的屬性值。 這樣的方式 不能重寫 WebViewClient  方法。不然點擊就變為 像這個 topicid://aa.bb:80/test?

p=12&d=1 Url 跳轉。

方式三:

        通過重寫 WebViewClient  。在 WebViewClient  中 有shouldOverrideUrlLoading(WebView view, String url)  的方法。能夠在裡面定義 Activity的跳轉 ,通過 自己定義超連結的Url 格式去進行不同的操作,如:http://www.mofangge.com 為 網頁跳轉 , blogJump://mofangge.com  連結形式為 向Activity 跳轉。通過 url  頭部 進行 loadUrl 還是startActivity 的跳轉推斷。

Demo下載:http://download.csdn.net/detail/jia635/8767179






著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

WebView 聯絡(要麼button)至 Activity 跳躍在幾個方面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.