android html 启动app,Android js交互 与 Html启动App
最近項目剛好有做到內部HTML頁面跳轉原生頁面和html代碼啟動App頁面的功能,做完之后覺得相關知識可以整理一下
先說下項目所用到的js交互
android調取JS的方法
WebView直接加載js的方法就好了
代碼如下
WebView.loadUrl("javascript:function(arg)")
html調用Android原生方法
//重點實現代碼
WebView.addJavascriptInterface(Object object,String name)
Objcet 對象是自己創建的對象,整體代碼如下
public class AndroidJs{
@JavascriptInterface
public void test(){}
}
//webview code
WebView.addJavascriptInterface(new AndroidJs(),“AndroidJs” )
//html code
window.AndroidJs.test()
外部HTML啟動APP頁面
使用Scheme方案設置
在AndroidManifest.xml文件中對應的頁面標簽添加如下
android:host="hostName"
android:path="path"
android:scheme="schemeName" />
Html代碼調用如下
//需要帶參數的和http鏈接的get請求一樣
帶參數的調用 在Activity中的獲取方式
String action = getIntent().getAction();
if(!TextUtils.isEmpty(action)&&Intent.ACTION_VIEW.equal(action)){
Uri uri = getIntent().getData();
if(uri != null){
String id = uri.getQueryParameter("id");
String name = uri.getQueryParameter("name");
}
}
自定義WebView處理scheme格式鏈接
public void loadUrl(String url){
if(isSchemeUrl(url)){
Intent intent = new Intent();
intent.setData(Uri.parse(url));
startActivity(intent);
}
}
private boolean isSchemeUrl(String url) {
if (TextUtils.isEmpty(url))
return false;
String[] strs = url.split("://");
if (strs.length > 1) {
String host = strs[0];
if (host.equalsIgnoreCase("http") || host.equalsIgnoreCase("https"))
return false;
else
return true;
} else return false;
}
一上是本人在處理Android JS交互 和 html啟動APP的一些心得,如有問題,請各位留言
總結
以上是生活随笔為你收集整理的android html 启动app,Android js交互 与 Html启动App的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt5.7+Opencv2.4.9人脸识
- 下一篇: Qt creator5.7 OpenCV