Android 解析 Intent 协议并打开程序 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

Android 解析 Intent 协议并打开程序

Android组件 sinvader 42573℃ 0评论

我现在写的程序是基于 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
具体关于这个的介绍(我怕链接没了,所以转载了 2017 年 12 月 01 日 19:15:35 更新: 乌云果然没了, 哎..):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

¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » Android 解析 Intent 协议并打开程序


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » Android 解析 Intent 协议并打开程序

喜欢 (5)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址