从深层链接检测打开的应用程序 - Android

时间:2017-09-30 14:16:19

标签: android android-activity deep-linking

我正在使用deep link到我的应用。如何检测我的应用程序是从<?xml version="1.0" encoding="UTF-8" standalone="no"?> <myroot xmlns="myxsd.xsd" class="15"> <data att1="all" att2="actual"> <myobject patt1="patt1_value" patt2="patt2_value" patt3="patt3_value"> <p name="p1">page1</p> <p name="p2">page1</p> <p name="p3">page1</p> <p name="p4">page4</p> </myobject> </data> </myroot> 打开还是从应用程序图标打开?

2 个答案:

答案 0 :(得分:1)

将您要在深层链接中使用的网址放入您的manifest.xml过滤器。

<activity
    android:name="com.example.android.GizmosActivity"
    android:label="@string/title_gizmos" >
    <intent-filter android:label="@string/filter_view_http_gizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/gizmos" />
        <!-- note that the leading "/" is required for pathPrefix-->
    </intent-filter>
    <intent-filter android:label="@string/filter_view_example_gizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "example://gizmos” -->
        <data android:scheme="example"
              android:host="gizmos" />
    </intent-filter>
</activity>

在您的活动中,您可以检查以下数据:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String action = intent.getAction();
    // using action u can detect 
    Uri data = intent.getData();
}

答案 1 :(得分:0)

在方法onCreate(savedInstanceState: Bundle?)中检查条件:

intent?.action == Intent.ACTION_VIEW && intent?.data != null