Android中app被其他app唤起

在 AndroidManifest.xml中定义scheme协议,如下

  <activity
            android:name="com.test.ForWebAcitivity"
            android:noHistory="true"
            android:screenOrientation="user"
            android:theme="@style/VideoStyle">
              <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <!--app可以被其他app唤起必须添加-->
                <category android:name="android.intent.category.BROWSABLE" />

                <!--app的scheme协议如下:apps://web2app:80000/test?
                scheme="fanxun:协议名称
                host="web2app:主机名称
                path="/test :路径
                path还可以替换为:pathPrefix和pathPattern。pathPrefix:只有路径的开头匹配pathPrefix中的内容即可。
                pathPattern:内容是正则表达式,路径匹配其规则即可。
                -->
                <data
                    android:path="/test"
                    android:port="80000"
                    android:host="web2app"
                    android:scheme="fanxun"
            </intent-filter>
        </activity>
  • 匹配符号:
    • “ * ” 用来匹配0次或更多,如:“a*” 可以匹配“a”、“aa”、“aaa”…
    • “.” 用来匹配任意字符,如:“.” 可以匹配“a”、“b”,“c”…
    • “.* ” 就是用来匹配任意字符0次或更多,如:“.*html” 可以匹配 “abchtml”、“chtml”,“html”,“sdf.html”…
    • 转义:因为当读取 Xml 的时候,“\” 是被当作转义字符的(当它被用作 pathPattern 转义之前),因此这里需要两次转义,读取 Xml 是一次,在 pathPattern 中使用又是一次。如:“” 这个字符就应该写成 “\”,“\” 这个字符就应该写成 “\\”。

在ForWebAcitivity类中获取传递过来的数据进行分析处理

  • 对于链接为( apps://web2app:80000/test?userId=6342ba83-d37d-47a5-b629-8c500d52a374&type=3&typeId=cc232ff8-a09e-4a1c-8217-5a83e5496874) 处理如下
 Uri uri = getIntent().getData();
        String userId = uri.getQueryParameter("userId");
            String type = uri.getQueryParameter("type");
            String typeId = uri.getQueryParameter("typeId");
            switch (type) {
               ...
            }
    

Uri,UR,URL的区别

  • URI类代表了一个URI(这个URI不是类,而是其本来的意义:通用资源标志符——Uniform Resource Identifier)实例。

  • Uri类是一个不可改变的URI引用,包括一个URI和一些碎片,URI跟在“#”后面。建立并且转换URI引用。而且Uri类对无效的行为不敏感,对于无效的输入没有定义相应的行为,如果没有另外制定,它将返回垃圾而不是抛出一个异常。Uri是Android开发的,扩展了JAVA中URI的一些功能来特定的适用于Android开发,所以大家在开发时,只使用Android 提供的Uri即可;

  • Resource Locator (URL) − 除了标识资源可用的位置之外,URI的一个子集描述了访问该资源的主要机制。

  • URL和URI的格式如:scheme:[//authority][/path][?query][#fragment]

    • scheme − 对于 URL, 是访问资源的协议名称;对其他URI,是分配标识符的规范的名称
    • authority − 可选的组成用户授权信息部分,主机及端口(可选)
    • path − 用于在scheme和authority内标识资源
    • query − 与路径一起的附加数据用于标识资源。对于url是查询字符串
    • fragment − 资源特定部分的可选标识符
  • Uri作为URI引用 ,封装了获取上述内容的方法。

  • URL和URI转换

  URI u = URI.create("http://somehost:80/path?thequery");
            //还可以这样初始化URLI
  URI uri1 = new URI("http://somehost:80/path?thequery");
  URL url = new URL("http://somehost:80/path?thequery");
     u.toURL();
     url.toURI();

猜你喜欢

转载自blog.csdn.net/genmenu/article/details/88691268