Android 从网页中跳转到本地App

gggggdf 7年前
   <p>我们在使用微信、QQ、京东等app的时候,会发现有时候通过他们的wap网页可以打开本地app,如果安装了则直接跳转,没有安装的话直接跳转应用商店</p>    <p>网页跳转app的原理如下:</p>    <p>对于Android平台URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。</p>    <p>格式如下:</p>    <pre>  <code class="language-java">scheme://host:port/path</code></pre>    <p>举个栗子:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/301801e3183809d823e893f5996b93d3.png"></p>    <p>URI栗子</p>    <p>下面看下data flag</p>    <pre>  <code class="language-java"><data android:host="string"         android:mimeType="string"         android:path="string"         android:pathPattern="string"         android:pathPrefix="string"         android:port="string"         android:scheme="string" /></code></pre>    <p>下面是一个测试demo,测试如何接收外部跳转:</p>    <h3>在我们的App入口Activity的清单文件中配置如下:</h3>    <pre>  <code class="language-java"><activity              android:name=".EntranceActivity"              android:launchMode="singleTask"              android:screenOrientation="portrait"              android:theme="@style/Entrance">              <intent-filter>                  <action android:name="android.intent.action.MAIN"/>                    <category android:name="android.intent.category.LAUNCHER"/>              </intent-filter>                <!--Android 接收外部跳转过滤器-->              <intent-filter>                  <!-- 协议部分配置 ,要在web配置相同的-->                  <data                      android:host="splash"                      android:scheme="test"/>                    <category android:name="android.intent.category.DEFAULT"/>                  <category android:name="android.intent.category.BROWSABLE"/>                    <action android:name="android.intent.action.VIEW"/>              </intent-filter>            </activity></code></pre>    <p>如上所示,在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "test://splash"的Uri。</p>    <h3>网页端需要配置如下</h3>    <pre>  <code class="language-java"><!DOCTYPE html>    <html>    <body>    <iframe src="test://splash" style="display:none"></iframe>    </body>    </html></code></pre>    <p>SO,当我们从网页跳转的App的时候,如果本地安装了,那么就可以顺利跳转过来了, 是不是感觉So easy 呢?</p>    <p>如果你想在单独处理外部跳转的Uri可以,在接收外部跳转的Activity中添加如下代码:</p>    <pre>  <code class="language-java">Intent intent = getIntent();          String data = intent.getDataString();          if (data.equals("yijj://splash")){              // TODO: 在这里处理你想干的事情。。。               startActivity(new Intent(this,EntranceActivity.class));          }else {              finish();          }</code></pre>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/356975d493bf</p>    <p> </p>