html call android apk的方法匯總

來源:互聯網
上載者:User

標籤:

現在很多應用都是混合型的,遇到一些Html頁面開啟APK的情境。那麼Html調用android有哪些方法呢?

1.如果是自己開發的瀏覽器APP,那麼html頁面用js直接和android java進行互動就可以實現。

2.如果是預設的系統瀏覽器訪問html,那麼可以選擇:

html js:window.location.herf="www.eg.com"

 

A:

<activity android:name=".DetailActivity" >  <intent-filter >    <data android:scheme="http" />
android:host="www.eg.com"
android:pathPrefix="/someresource/" <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter></activity>

  可以看到,android是從intent的配對進位來進行過濾和選擇的,其中和連結對應的地址我們有兩個選擇,custom scheme可以是網址的形式,比如www.example.com或者是自訂的內容,比如my.custom.scheme。

這種方法和ios端html call app的做法非常相似,html部分可以保持代碼一致。

自訂的內容,關聯很強,使用者點選連結只會開啟scheme吻合的APP,其中intent機制起過濾作用,地址要完全吻合host,pathPrefix等過濾條件。http的通用scheme不能保證直接開啟到你的APP,可能會顯示不同的應用選擇彈窗。

自訂內容可能因為安全性的原因,在4.4版本測試的時候,window.location.herf不能開啟APP,使用者手動點選連結卻可以。

連結地址的傳參可以用www.eg.com/read?param1=xxx&param2=xxx進行傳遞。java端getIntent.getData 擷取到Uri之後進行解析參數。

 

B:android自己獨特的方法:

html:

<a href="intent:#Intent;action=com.appname.CUSTOM_ACTION;S.para1=123456;i.number=4;end">click to open</a>

java:

<activity    android:name=".MyActivity"    android:screenOrientation="sensorLandscape" >    <intent-filter>        <action android:name="com.appname.CUSTOM_ACTION" />        <category android:name="android.intent.category.DEFAULT" />        <category android:name="android.intent.category.BROWSABLE" />    </intent-filter></activity>

intent 自訂的URI格式如下

intent:
   HOST/URI-path // Optional host 
   #Intent; 
      package=[string]; 
      action=[string]; 
      category=[string]; 
      component=[string]; 
      scheme=[string]; 
   end; 

參數的聲明格式為type.varName的方式,分號;串連 

<type>.<key>=<value>

 

S =StringB =Booleanb =Bytec =Characterd =Doublef =Floati =Integerl =Longs =Short

比如S.str=sss;i=123;

adb 命令列debug的方法:

 

adb shell am start -a android.intent.action.VIEW -d "http://example.com/gizmos" com.example.android

其中-a -d -c 等分別代表action data category 

html call android apk的方法匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.