Scheme協議詳細介紹

目錄介紹

  • 1.URL Scheme使用場景介紹
  • 2.URL Scheme基礎介紹php

    • 2.1 什麼是URL Scheme?
    • 2.2 URL Scheme協議格式解釋
    • 2.3 Scheme連接格式樣式
  • 3.URL Scheme如何使用android

    • 3.1 設置Scheme
    • 3.2 獲取Scheme跳轉的參數,並添加跳轉方式
    • 3.3 調用方式
    • 3.4 如何判斷一個Scheme是否有效
    • 3.5 Scheme在短信息中注意要點

關於Scheme應用案例

關於連接

1.URL Scheme使用場景介紹

  • URL Scheme使用場景,目前1,2,5使用場景很廣,有沒有一種熟悉的感受?git

    * 1.經過小程序,利用Scheme協議打開原生app
    * 2.H5頁面點擊錨點,根據錨點具體跳轉路徑APP端跳轉具體的頁面
    * 3.APP端收到服務器端下發的PUSH通知欄消息,根據消息的點擊跳轉路徑跳轉相關頁面
    * 4.APP根據URL跳轉到另一個APP指定頁面
    * 5.經過短信息中的url打開原生app

2.URL Scheme基礎介紹

2.1 什麼是URL Scheme?

  • android中的scheme是一種頁面內跳轉協議,是一種很是好的實現機制,經過定義本身的scheme協議,能夠很是方便跳轉app中的各個頁面

2.2 URL Scheme協議格式

String urlStr="http://www.ycbjie.cn:80/yc?id=hello&name=cg";
//url =            protocol + authority(host + port) + path + query
//協議protocol=    http
//域名authority=   www.ycbjie.cn:80
//頁面path=          /yc
//參數query=       id=hello&name=cg
//authority =      host + port
//主機host=        www.ycbjie.cn
//端口port=        80

2.3 Scheme連接格式樣式

  • 樣式:[scheme]://[host]/[path]?[query]

3.URL Scheme如何使用

3.1 設置Scheme

  • 在AndroidManifest.xml中對標籤增長設置Scheme
<activity
    android:name=".ui.main.ui.activity.SchemeFirstActivity"
    android:screenOrientation="portrait">
    <!--Android 接收外部跳轉過濾器-->
    <!--要想在別的App上能成功調起App,必須添加intent過濾器-->
    <intent-filter>
        <!-- 協議部分配置 ,注意須要跟web配置相同-->
        <!--協議部分,隨便設置 yc://ycbjie:8888/from?type=yangchong  -->
        <data android:scheme="yc"
            android:host="ycbjie"
            android:path="/from"
            android:port="8888"/>


        <!--下面這幾行也必須得設置-->
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
</activity>

3.2 獲取Scheme跳轉的參數,並添加跳轉方式

public class SchemeFirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri uri = getIntent().getData();
        if (uri != null) {
            //獲取指定參數值
            String type = uri.getQueryParameter("type");
            Log.e( "UrlUtils","main: " + type);

            if(type.equals("yangchong")){
                ActivityUtils.startActivity(GuideActivity.class);
            }else if(type.equals("main")){
                ActivityUtils.startActivity(MainActivity.class);
            }
        }
        finish();
    }
}

3.3 調用方式

  • 3.3.1 原生調用
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("yc://ycbjie:8888/from?type=yangchong"));
startActivity(intent);
  • 3.3.2 網頁調用
<a href="yc://ycbjie:8888/from?type=yangchong">打開叮咚app</a>
  • 3.3.3 短信息中調用

3.4 如何判斷一個Scheme是否有效

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("yc://ycbjie:8888/from?type=yangchong"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
    startActivity(intent);
}

3.5 Scheme在短信息中注意要點

  • 設置android:scheme="http"或者android:scheme="https"後,點擊短信息或者h5頁面,發現沒有跳到指定的頁面,反而打開的是網頁連接。

關於個人博客

相關文章
相關標籤/搜索