data属性的妙用

一、data的格式

data的属性有:scheme,host,port,path

其中这些属性和IP地址访问格式是一一对应的,例如:
  ip地址: http://192.168.1.1:80/hello
  data: scheme="http",host="192.168.1.1",port="80",path="/hello"

注意:data的scheme不限定于http,可以自定义的任一单词。

二、data使activity跳转

使用data实现activity的跳转,实际上就是给activity赋值一个IP地址,通过该地址可以访问该activity。

这里主要讲两种跳转:HTML跳转和activity跳转。

首先对跳转到的activity进行设置,赋予该activityIP地址,确保跳转的时候能找到该activity。

<activity
    android:name=".SecondActivity"
    android:configChanges="keyboardHidden|orientation|keyboard">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <!--IP地址:start://nihao:8000/hello-->
        <data
            android:host="nihao"
            android:path="/hello"
            android:port="8000"
            android:scheme="start" />
    </intent-filter>
</activity>

注:当使用data实现跳转的时候,action和category是必须要设置的,而且不可变。
action该activity可以执行的动作,category指定当前动作(Action)被执行的环境。
  android.intent.action.VIEW用于显示用户的数据即data,比较通用,会根据用户的数据类型打开相应的Activity。
  android.intent.category.DEFAULTAndroid系统中默认的执行方式,按照普通Activity的执行方式执行。表示所有intent都可以激活它。
  android.intent.category.BROWSABLE设置该组件可以使用浏览器启动。

 HTML跳转:
<a href="start://nihao:8000/hello?goodsId=10011002">打开</a>

注意:webview.setWebViewClient();方法会将该链接当成一个IP地址,直接在webview中去访问,不会提交给系统的浏览器处理,就不能实现页面的跳转。这时候需要在WebViewClient的shouldOverrideUrlLoading()中进行拦截处理,通过startActivity(action,url)实现跳转。

 直接通过webview里的网址跳转,在Activity中可通过uri获取goodsId的值:

String goodsId = uri.getQueryParameter("goodsId");
 activity跳转:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("start://nihao:8000/hello?goodsId=10011002"));
startActivity(intent);

 直接通过webview里的网址跳转,在Activity中可通过uri获取goodsId的值:

String goodsId = uri.getQueryParameter("goodsId");

参考:
Intent属性详解二 Action、Category
Intent及其七大属性及intent-filter设置

posted @ 2018-11-22 12:00  陌上书码  阅读(353)  评论(0)    收藏  举报