网页跳转回应用的实现原理。

就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下:
scheme://host:port/path
举个实际的例子:
content://com.example.project:200/folder/subfolder/etc
\---------/ \---------------------------/ \---/ \--------------------------/
scheme host port path
\--------------------------------/
authority

现在大家应该知道data flag中那些属性的含义了吧,看下data flag
<data android:host="string"
android:mimeType="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:port="string"
android:scheme="string" />

以下是测试示例的实现步骤:

1. 新建一个工程A,并实现一个Activity,用来接收外部的跳转。代码如下:

[java] view plain copy
  1. publicclassJumpActivityextendsActivity{
  2. @Override
  3. protectedvoidonCreate(BundlesavedInstanceState){
  4. //TODOAuto-generatedmethodstub
  5. super.onCreate(savedInstanceState);
  6. Intentintent=getIntent();
  7. Stringdata=intent.getDataString();
  8. System.out.println(data);
  9. }
  10. }

在这个Activity里,我们可以通过getIntent(),来获取外部跳转传过来的信息。

2. 修改ManiFest文件,设置Activity的接收Action的属性,代码如下:

[java] view plain copy
  1. <activityandroid:name=".JumpActivity">
  2. <intent-filter>
  3. <actionandroid:name="android.intent.action.VIEW"/>
  4. <categoryandroid:name="android.intent.category.DEFAULT"/>
  5. <categoryandroid:name="android.intent.category.BROWSABLE"/>
  6. <data
  7. android:host="data"
  8. android:scheme="sharetest"/>
  9. </intent-filter>
  10. </activity>

此处注意:

  1. <data
  2. android:host="data"
  3. android:scheme="sharetest"/>

不能和定义android.intent.category.LAUNCHER的intent-filter放在一起,要分开放如下:

      <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>            <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="<span style="color: rgb(0, 0, 255); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">sharetest</span>" android:host="<span style="color: rgb(0, 0, 255); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 14px; line-height: 18px;">data</span>"/>            </intent-filter>
这样就可以在android手机浏览器直接输入类似于 "sharetest://data/XXX"的链接来跳转app了

如上所示,在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接。

3. 接下来我们可以写一个测试工程B,看看工程A中的JumpActivity是否可以接收和处理类似于 "sharetest://data/XXX"的链接。因此,新建一个工程B,工程里只有一个WebView,用来打开一个本地的网页。由于WebView在接收到自己不能处理的Uri时,会自动的调用发送Intent来调用能够处理该Uri的应用。

以下是网页的代码:

[html] view plain copy
  1. <!DOCTYPEhtml>
  2. <html>
  3. <body>
  4. <iframesrc="sharetest://data/123"style="display:none"></iframe>
  5. </body>
  6. </html>
将该网页放到Assets目录下,在代码里调用Webview加载该Html文件,代码如下:

[java] view plain copy
  1. publicvoidloadHtml()
  2. {
  3. WebViewwebview=newWebView(this);
  4. WebSettingswSet=webview.getSettings();
  5. wSet.setJavaScriptEnabled(true);
  6. webview.loadUrl("file:///android_asset/a.html");
  7. setContentView(webview);
  8. }

4. 将手机装上工程A和工程B。打开工程A,可以发现:工程B的JumpActivity会自动被打开。如下图所示:在Intent中可以去到网页里发出的链接:sharetest://data/123。

因此,我们就可以在JumpActivity里处理接收到的消息了。


通过以上的4步,相信大家都了解如何让应用接收和处理某种Uri消息。因此,只要我们

1. 在ManiFest里为Activity申明接收并处理某种Uri

2. 在页面中添加以下代码:其中,l为符合Activity处理规则的Uri地址。

[java] view plain copy
  1. <iframesrc="l"style="display:none"></iframe>
就可以让已安装该应用的用户在打开该网页的时候,自动的跳转到应用内部。

点击微信和QQ分享跳转到程序内部的原理与此一致。

更多相关文章

  1. Android(安卓)8.0 高通代码预制apk可卸载,恢复出厂设置apk可恢复
  2. Android9.0 SIM卡初始化---更新数据(代码 高通9.0代码)
  3. 极光推送Android集成以及使用
  4. android Toast大全
  5. 如何在代码里打开Android手机通知状态栏
  6. Android2.1 短信应用解析
  7. 工程android配置windows7下cocos2d-x、android的开发环境
  8. Android百度地图之页面跳转
  9. Android开源SlidingMenu的使用

随机推荐

  1. Apollo 源码解析 —— Portal 创建 Item
  2. Apollo 源码解析 —— Portal 灰度全量发
  3. Apollo 源码解析 —— Portal 认证与授权
  4. Apollo 源码解析 —— Admin Service 发
  5. Apollo 源码解析 —— Client 轮询配置
  6. 艿艿连肝了几个周末,写了一篇贼长的 Sprin
  7. 艿艿周末又肝了一篇万字 Dubbo 文章,Sprin
  8. PMP备考经历-心理篇
  9. 又肝了下微服务 API 网关“金刚”,也是蛮
  10. 又一国产微服务网关 Soul,一起来肝一肝,Dub