标签:
先说实现步骤再说原理:
使用步骤
一,首先要给你要打开的应用中的activity设置过滤器(在清单文件里设置)
以JumpActivity为例
如下面的: <intent-filter> 中就是所需过滤器
<activity android:name=".JumpActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--下面所设置的质需要和html端对调-->
<!--在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接-->
<data
android:host="data"
android:scheme="sharetest" />
</intent-filter>
</activity> public class JumpActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();//在这个Activity里,我们可以通过getIntent(),来获取外部跳转传过来的信息。
String data = intent.getDataString();//接收到网页传过来的数据:sharetest://data/http://www.huxiu.com/
String[] split = data.split("data/");//以data/切割data字符串
url = split[1]; //就得到:http://www.huxiu.com/(这就是我们需要网页传给我们的数据)
。。。然后我们再通过网页打开app的同时就可以用获得的url数据做一些我们需要做的处理
比如你在微信里浏览网页时打开自己的安卓app应用的同时,加载一个app内的网页
}
} <iframe src="" style="display:none"></iframe>
如下:index.html
<!DOCTYPE html> <html> <body> <iframe src="sharetest://data/http://www.huxiu.com/" style="display:none"></iframe> </body> </html>
public class H5ToAppActivity extends Activity {
private String url;
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_h5_app);
webview = (WebView) findViewById(R.id.webviewh5);
url = "file:///android_asset/index.html";
WebSettings wSet = webview.getSettings();
wSet.setJavaScriptEnabled(true);
webview.loadUrl(url);
}
}下面是两个应用截图你可以下载下来看下效果:(两个应用一起下)
2048网页演示apk:http://download.csdn.net/detail/qiushi_1990/9514778
网页打开的应用apk:http://download.csdn.net/detail/qiushi_1990/9514779
这样在打开2048时会出现下面效果
然后会跳转到下面应用
实现原理
最近,在使用QQ和微信等SDK来实现分享网页的时候,发现,SDK已经为页面跳转回应用提供了基本的数据支持。我们只需在应用里和被分享的网页进行简单的设置,即可实现此功能。
那么我们先来看下网页跳转回应用的实现原理。
就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" />
标签:
原文地址:http://blog.csdn.net/qiushi_1990/article/details/51355073