Ted
Ted

Reputation: 23756

Open deeplink with parameters on android

Following the intent format in https://developer.chrome.com/docs/multidevice/android/intents/

intent:  
  HOST/URI-path  
  #Intent;  
    package=\[string\];  
    action=\[string\];  
    category=\[string\];  
    component=\[string\];  
    scheme=\[string\];  
  end;

We are sending HOST/URI-path: com.example/somePath?someVariableA=1

e.g. intent://com.example/somePath?someVariableA=1#Intent;package=com.example;scheme=app;end;

The android app receives data = com.example/somePath however we are missing someVariableA=1.

How can we get someVariableA=1?

Upvotes: 1

Views: 427

Answers (1)

James Silva
James Silva

Reputation: 1

When a deeplink is opened in an Android app, the parameters can be retrieved from the intent data URI.

Below is a way how you can achieve this in your Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    Uri data = intent.getData();

    if (data != null) {
        List<String> parameters = data.getPathSegments();
        String paramA = "";

        if (parameters. Size() > 0) {
            paramA = parameters.get(0); // get first parameter
        }

        // Extracting query parameters
        String someVariableA = data.getQueryParameter("someVariableA"); 

        // Now you have your someVariableA
    }
}

In the code snippet above, we're getting the Intent that started the Activity and then extracting the data Uri. The Uri class has methods to parse the different parts of the URI, including the path segments and query parameters.

The getQueryParameter(String key) method can be used to retrieve the value of "someVariableA=1" by calling data.getQueryParameter("someVariableA").

Remember, this code should be placed in the Activity that is started by the intent from the deeplink. In case the Activity might be started from elsewhere, you should add null checks as necessary to prevent NullPointerExceptions.

Upvotes: 0

Related Questions