Android 中Scheme协议的使用详解

    一、首先什么是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)

    <a href="xl://goods:8888/goodsDetail?goodsId=10011002">打开商品详情</a>

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

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

<activity
           android:name=".SchemeActivity"
           android:label="@string/app_name">
           <!-- 要想在别的App上能成功调起App,必须添加intent过滤器 -->
               <!-- 协议部分,随便设置 -->
               <intent-filter>
                   <!--协议部分,随便设置-->
                   <data android:scheme="scheme" android:host="mtime" android:path="/goodsDetail" />
                   <!--下面这几行也必须得设置-->
                   <category android:name="android.intent.category.DEFAULT"/>
                   <action android:name="android.intent.action.VIEW"/>
                   <category android:name="android.intent.category.BROWSABLE"/>
               </intent-filter>
       </activity>

    (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<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);

    boolean isValid = !activities.isEmpty();

    if (isValid) {

    startActivity(intent);

    }

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

    <activity android:name=".ActivityAAAAAA">

       <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="app"/*没有配置host 和path*/

            />

       </intent-filter>

   </activity>

    <activity android:name=".ActivityBBBBBB">

       <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="app"

            android:host="com.bobo.package"

           />

       </intent-filter>

    </activity>

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

    <activity android:name=".ActivityName">

       <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="app"

            android:host="com.bobo.package"

            android:path="/path"/>

        <data android:scheme="application"

           android:host="host"

           android:path="/route"/>

      </intent-filter>

   </activity>

猜你喜欢

转载自blog.csdn.net/ZytheMoon/article/details/101674939