首先,要给你要打开的应用中的activity设置过滤器,一般是启动的activity。
1 |
<activity android:name=".JumpActivity" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!--下面所设置的质需要和html端对调--> <!--在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接--> <data android:host="data" android:scheme="sharetest" /> </intent-filter> </activity> |
然后,在JumpActivity中做打开后的处理,用来接收外部的跳转。
1 |
作者:编程小石头 链接:https://zhuanlan.zhihu.com/p/62840092 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 public class JumpActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Intent intent = getIntent(); String data = intent.getDataString(); String[] split = data.split("data/"); url = split[1]; //然后我们再通过网页打开app的同时就可以用获得的url数据做一些我们需要做的处理 // 比如你在微信里浏览网页时打开自己的安卓app应用的同时,加载一个app内的网页 } } |
然后,新建一个index.html。
1 |
<!DOCTYPE html> <html> <body> <iframe src="sharetest://data/http://www.huxiu.com/" style="display:none"></iframe> </body> </html> |
下面是一段测试代码:
1 |
public class H5ToAppActivity extends Activity { private String url; private WebView webview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_h5_app); webview = (WebView) findViewById(R.id.webviewh5); url = "file:///android_asset/index.html"; WebSettings wSet = webview.getSettings(); wSet.setJavaScriptEnabled(true); webview.loadUrl(url); } } |