最近开发sdk的时候遇到M页传参到app的问题,目前经研究有如下两种方式:
一,采用openapp协议
1,在AndroidManifest.xml中声明接收请求的activity:
<activity android:name=".OpenAppActivity">
<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="myapp" android:host="demo.app" android:pathPrefix="/openwith"/>
</intent-filter>
</activity>
2,M页:
<body>
<a href='myapp://demo.app/openwith?data=testparam'>click here</a>
</body>
二,app调用js,js再回调app
1,声明js方法
public class JSInterfaceDemo{
public JSInterfaceDemo() {
}
@JavascriptInterface
public void jsMethodDemo(){
System.out.println("调用成功!");
}
}
2,app跳M页之前,注册js接口:
WebView wv_content = (WebView)findViewById(R.id.webView1);
WebSettings setting=wv_content.getSettings();
setting.setJavaScriptEnabled(true);
setting.setUseWideViewPort(true);
setting.setLoadWithOverviewMode(true);
setting.setJavaScriptCanOpenWindowsAutomatically(true);
wv_content.setWebChromeClient(new WebChromeClient());
wv_content.loadUrl("file:///android_asset/innerM.html");
wv_content.addJavascriptInterface(new JSInterfaceDemo(this), "alias");
3,M页调用事先注册好的js方法
<body>
<a href="#" onclick="window.alias.jsMethodDemo()">demo</a>
</body>