Android解析Intent协议并打开程序

来源:互联网 发布:ceic数据库与wind 编辑:程序博客网 时间:2024/06/15 03:27

我现在写的程序是基于webview的,程序中又遇到了网页版支付宝支付,里面就有这么一个链接,这个链接在访问之后,会打开支付宝程序:

  1. intent://platformapi/startapp?appId=20000013&pwdType=ordinaryPassword&_t=1456301771669#Intent;scheme=alipays;package=com.eg.android.AlipayGphone;end

看下这个东西是怎么生成的:http://bbs.mobiletrain.org/thread-31234-1-1.html
具体关于这个的介绍(我怕链接没了,所以转载了):Intent scheme URL attack或者原文:http://drops.wooyun.org/papers/2893
人家是黑程序的,不过最后也好心给了上面的链接解析的方法:

  1. // convert intent scheme URL to intent object
  2. Intent intent = Intent.parseUri(uri);
  3. // forbid launching activities without BROWSABLE category
  4. intent.addCategory("android.intent.category.BROWSABLE");
  5. // forbid explicit call
  6. intent.setComponent(null);
  7. // forbid intent with selector intent
  8. intent.setSelector(null);
  9. // start the activity by the intent
  10. context.startActivityIfNeeded(intent, -1);

上面是前几年的代码,现在已有所改动,Intent.parseUri参数已经变了,参看下面的代码:

  1. Intent intent;
  2. try {
  3. intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
  4. // forbid launching activities without BROWSABLE
  5. // category
  6. intent.addCategory("android.intent.category.BROWSABLE");
  7. // forbid explicit call
  8. intent.setComponent(null);
  9. // forbid intent with selector intent
  10. intent.setSelector(null);
  11. // start the activity by the intent
  12. startActivityIfNeeded(intent, -1);
  13. } catch (URISyntaxException e) {
  14. // TODO Auto-generated catch block
  15. e.printStackTrace();
  16. }

亲测成功

  1. mNewWebView.setWebViewClient(new WebViewClient() {
  2. @Override
  3. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  4. // intent://platformapi/startapp?appId=20000013&pwdType=ordinaryPassword&_t=1456301771669#Intent;scheme=alipays;package=com.eg.android.AlipayGphone;end
  5. if (url.startsWith("intent://")) {
  6. Intent intent;
  7. try {
  8. intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
  9. // forbid launching activities without BROWSABLE
  10. // category
  11. intent.addCategory("android.intent.category.BROWSABLE");
  12. // forbid explicit call
  13. intent.setComponent(null);
  14. // forbid intent with selector intent
  15. intent.setSelector(null);
  16. // start the activity by the intent
  17. startActivityIfNeeded(intent, -1);
  18. } catch (URISyntaxException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. return true;
  23. }
  24. view.loadUrl(url);
  25. return true;
  26. }
  27. });

如果想要尝试的话可以用上面的webview访问:https://mobile.alipay.com/index.htm?cid=wap_dc

转载请注明:sumile » Android解析Intent协议并打开程序

文章为从我的博客迁移过来的,所以标注为原创

0 0