一、首先什么是Scheme?

 

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

    客户端应用可以向系统注册一个 URL Scheme,该Scheme用于从浏览器或其他应用中启动本应用。通过指定的 URL 字段,可以让应用在被调起后直接打开某些特定页面。也可以执行某些指定动作。综上Scheme使用场景大致分以下几种:

    1.服务器下发跳转路径,客户端根据服务器下发跳转路径跳转相应的页面

    2.H5页面点击锚点,根据锚点具体跳转路径APP端跳转具体的页面

    3.APP端收到服务器端下发的PUSH通知栏消息,根据消息的点击跳转路径跳转相关页面

    4.APP根据URL跳转到另外一个APP指定页面

 

    二、Scheme协议格式

 

    上面说到Scheme是一种页面内跳转协议,那协议一定有固定的格式,在Android中Scheme属于Uri,下面就是Scheme的协议结构:

    [scheme:]scheme-specific-part[#fragment]

    [scheme:][//authority][path][?query][#fragment]

     String url="http://www.orangecpp.com:80/tucao?id=hello&name=lily";

    //url = protocol + authority(host + port) + path + query

    //协议protocol= http

    //域名authority= www.orangecpp.com:80

    //主机host= www.orangecpp.com

    //端口port= 80

    //页面path= /tucao

    //参数query= id=hello&name=lily

    在android中,除了scheme、authority是必须要有的,其它的几个path、query、fragment,它们每一个可以选择性的要或不要,但顺序不能变

 

    三、Scheme的使用方式

 

     Scheme的使用分为两个部分,在调用方传入Intent以及被调用方设置拦截并解析数据:

    1.调用方使用Scheme的方式,基本有两种:

    (1)

    Uri uri = Uri.parse(schemeStr);

    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

    startActivity(intent);

    (2)

    打开商品详情

 

    2.AndroidManifest中设置增加拦截器(intent-filter),增加设置scheme,在SchemeActivity中通过重写onNewIntent方法获取Scheme跳转参数

 

    (1)AndroidManifest中设置增加拦截器(intent-filter),设置scheme

                                                                                                                                                                                 

    (2)在SchemeActivity中获取参数

public class SchemeActivity extends Activity {    private static final String TAG = "SchemeActivity";    private TextView schemeTv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_scheme);        schemeTv = (TextView) findViewById(R.id.scheme_tv);        Uri data = getIntent().getData();        Log.i(TAG, "host = " + data.getHost() + " path = " + data.getPath() + " query = " + data.getQuery());        String param = data.getQueryParameter("goodsId");        schemeTv.setText("获取的参数为:" + param);    }}

 

    四、在使用过程中会遇到的情况

    

    (1)判断Scheme是否有效

    PackageManager packageManager = getPackageManager();

    Intent intent = new Intent(Intent.ACTION_VIEW,

    Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));

    List activities = packageManager.queryIntentActivities(intent, 0);

    boolean isValid = !activities.isEmpty();

    if (isValid) {

    startActivity(intent);

    }

    (2)如下两个Activity 当通过scheme 跳转界面时 ,系统会提示选择打开方式 因为没有精确匹配要跳哪个界面

   

       

         

         

         

         

            />

       

   

 

   

       

         

         

         

         

            android:host="com.bobo.package"

           />

       

   

    (3)如果不同的链接都要跳到一个Activity,Activity的配置

   

       

         

         

         

         

            android:host="com.bobo.package"

            android:path="/path"/>

       

           android:host="host"

           android:path="/route"/>

     

   

 

 

更多相关文章

  1. 安全新手入坑——HTML标签
  2. Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
  3. 关于GitHub安装路径及Android(安卓)Studio中的配置
  4. 深入了解Android中文件路径问题
  5. Android本地存储目录研究
  6. android html唤醒APP(原生)
  7. Android:让WebView支持元素,实现文件上传
  8. Andorid的Uri获得图片,视频等
  9. Android与JavaScript交互

随机推荐

  1. Android之Spinner用法
  2. Android(安卓)adb.exe程序启动不起来
  3. Android(安卓)消息通知栏Notification使
  4. Android(安卓)8.1 通知的变化
  5. Android连接WiFi
  6. Android(安卓)studio之启动AVD模拟器异常
  7. android添加监听器
  8. android studio获取SH1码
  9. Android(安卓)Camera Subsystem - startP
  10. android TelephonyManager