关于android app跳转和从web跳转的问题(转载修正)
网页跳转回应用的实现原理。
就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- publicclassJumpActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- Intentintent=getIntent();
- Stringdata=intent.getDataString();
- System.out.println(data);
- }
- }
在这个Activity里,我们可以通过getIntent(),来获取外部跳转传过来的信息。
2. 修改ManiFest文件,设置Activity的接收Action的属性,代码如下:
[java] view plain copy
- <activityandroid:name=".JumpActivity">
- <intent-filter>
- <actionandroid:name="android.intent.action.VIEW"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- <categoryandroid:name="android.intent.category.BROWSABLE"/>
- <data
- android:host="data"
- android:scheme="sharetest"/>
- </intent-filter>
- </activity>
此处注意:
- <data
- android:host="data"
- 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- <!DOCTYPEhtml>
- <html>
- <body>
- <iframesrc="sharetest://data/123"style="display:none"></iframe>
- </body>
- </html>
[java] view plain copy
- publicvoidloadHtml()
- {
- WebViewwebview=newWebView(this);
- WebSettingswSet=webview.getSettings();
- wSet.setJavaScriptEnabled(true);
- webview.loadUrl("file:///android_asset/a.html");
- setContentView(webview);
- }
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
- <iframesrc="l"style="display:none"></iframe>
点击微信和QQ分享跳转到程序内部的原理与此一致。
更多相关文章
- Android(安卓)8.0 高通代码预制apk可卸载,恢复出厂设置apk可恢复
- Android9.0 SIM卡初始化---更新数据(代码 高通9.0代码)
- 极光推送Android集成以及使用
- android Toast大全
- 如何在代码里打开Android手机通知状态栏
- Android2.1 短信应用解析
- 工程android配置windows7下cocos2d-x、android的开发环境
- Android百度地图之页面跳转
- Android开源SlidingMenu的使用