如何通过从外部链接单击来打开特定ID的特定活动

时间:2018-11-03 21:40:30

标签: android android-intent intentfilter

我在我的应用程序中添加了应用程序链接助手,以通过外部链接打开特定的活动。现在,我的DataDetailActivity中包含以下代码。

   Intent appLinkIntent = getIntent();
    String appLinkAction = appLinkIntent.getAction();
    Uri appLinkData = appLinkIntent.getData();
    if(appLinkData != null)
    {
        String dataId = appLinkData.getLastPathSegment();

        Intent resultIntent = new Intent(this, DataDetailActivity.class);
        startActivity(resultIntent);

    }

我想打开特定的DataDetailActivity。但这确实会引发错误。我必须在哪里将dataId传递给意图?

注意:我们的应用程序具有“登录到FB”按钮的“默认主屏幕”。那么,我是否必须在HomeScreenActivity中写下此代码并将意图传递给DatadetailAcvity.java?如果是,那我如何告知DataDetailScreen特定数据(如果我有ID)?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

manifests.xml

<application
    ....
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name=".HomeScreenActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">
        <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="somescheme" />  <!--This line to define schema -->
        </intent-filter>
    </activity>

 ....
</application>

HomeScreenActivity

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

private void checkDeepLink(){
    if (getIntent() != null && getIntent().getData() != null) {
        Uri data = getIntent().getData();
        String scheme = data.getScheme();
        String host = data.getHost();
        String param = data.getQuery();
        Log.d("DeepLink","Schema : " + scheme);
        Log.d("DeepLink","Host : " + host);
        Log.d("DeepLink","param : " + host);

        if (host.equals("page_details")){
            Intent intent = new Intent(this,DatadetailAcvity.class);
            intent.putExtra("detail_id",Long.valueOf(data.getQueryParameter("detail_id")));  // URL query values as string, you need to parse string to long.
            startActivity(intent);
        }else{
          // ... other logic
        }
    }
}

DeepLink

scheme://host?pama_name=value&other_param_name=value

示例:

 somescheme://page_details?detail_id=2

Facebook Step-by-Step Guide

更新

DatadetailAcvity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_detail);
    if (getIntent() !=null) {
        long detailId = getIntent().getLongExtra("detail_id",-1);

        if (detailId != -1){
            // do your stuff and displayed page by id
        }
    }
}

答案 1 :(得分:0)

没有错误信息,但是如果您想通过深度链接传递数据,则可以使用查询参数。 假设网址为https://www.example.com/example?param1=hello

然后

Intent intent = getIntent();
Uri data = intent.getData();
String param1 = data.getQueryParameter("param1");