zoukankan      html  css  js  c++  java
  • Android使用URL Scheme唤起APP

    现如互联网的发展,单个app或者单个平台很难满足公司业务的需求,多平台合作或协作是大势所趋。并且未来IT业务的发展形势必然要朝着平台化共享化的方向发展。业务对于平台的依赖也会越来越强。平台之间的数据交换和共享是必然会发生的事情。

    扯远了,把视线回归到APP本身。不同的APP之间也是需要进行数据交互。举个栗子简单说下URL Scheme

    假如有这么一个场景,公司由于业务的发展,需要app用户在app内进行实名认证,恰好支付宝平台提供芝麻实名认证功能。那么app就需要进行唤起支付宝app进行实名认证,实名认证完毕之后支付宝返回实名认证结果。这就完成了不同app之间进行了数据交互。

    URL Scheme:

      android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

    URL Scheme协议格式:

    栗子1:
    http://baidu:8080/news?system=pc&id=45464
    
    栗子2:
    alipays://platformapi/startapp?appId=20000067

    http 代表Scheme的协议

    baidu代表作用于哪个地址域

    8080代表路径的端口号

    news代表Scheme指定的页面

    system和id代表要传递的参数

    URL Scheme使用方法:

    首先要在Mainifest文件中对要启动的activity进行添加过滤器。

    <activity
        android:name="com.example.helloworld.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </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="http" android:host="baidu" android:path="/news" android:port="8080"/>
    </intent-filter> </activity>

    在activity中进行接收参数:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent intent = getIntent();
            String scheme = intent.getScheme();
            String dataString = intent.getDataString();
            Uri uri = intent.getData();
            System.out.println("scheme:" + scheme);
            if (uri != null) {
                //完整的url信息
                String url = uri.toString();
                //scheme部分
                String schemes = uri.getScheme();
                //host部分
                String host = uri.getHost();
                //port部分
                int port = uri.getPort();
                //访问路径
                String path = uri.getPath();
                //编码路径
                String path1 = uri.getEncodedPath();
                //query部分
                String queryString = uri.getQuery();
                //获取参数值
                String systemInfo = uri.getQueryParameter("system");
           String id=uri.
    getQueryParameter("id"); 
           System.out.println(
    "host:" + host);
           System.out.println(
    "dataString:" + dataString);
           System.out.println(
    "id:" + id);
           System.out.println(
    "path:" + path);
           System.out.println(
    "path1:" + path1);
           System.out.println(
    "queryString:" + queryString);
        }
      }
    }

    调用方式:

    网页上调用

    <a href="http://baidu:8080/news?system=pc&id=45464">test</a>

    native调用

    if (hasApplication()) {
    Intent action = new Intent(Intent.ACTION_VIEW);
    StringBuilder builder = new StringBuilder();
    builder.append("http://baidu:8080/news?system=pc&id=45464");
    action.setData(Uri.parse(builder.toString()));
    startActivity(action);
    }
    
    
    /**
    * 判断是否安装了应用
    * @return true 为已经安装
    */
    private boolean hasApplication() {
            PackageManager manager = mContext.getPackageManager();
            Intent action = new Intent(Intent.ACTION_VIEW);
            action.setData(Uri.parse("http://"));
            List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
            return list != null && list.size() > 0;
        }
  • 相关阅读:
    Flex中States的用法
    MAX脚本翻译教学
    WARN No appenders could be found for logger 解决
    解压版(绿色版)Tomcat配置
    Bootstrap入门
    什么时候用margin、padding
    简易的商品统计
    块级元素&行内元素
    不定宽元素水平居中
    JavaScript与表单交互(表单验证模型)
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/7093769.html
Copyright © 2011-2022 走看看