How to give Intent Data in Deep Link - android

I have updated manifest file to support deep linking. I have checked it from Run(Edit Configuration), and it opens the specified activity.
Now i need to send some data with the deep link. So what should be the procedure of it. I have added another data attribute, but i don't understand how to get data in the Activity in the same key/value fashion.
I am getting Intent like this in Activity
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
intent.getData() has this value= myapp://videodeeplink
I have read some articles and tutorials regarding this, but i am just unable to get this. Kindly guide me how to put and get some data in deep linking.
myapp://videodeeplink
<activity
android:name=".VideosListActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myapp" android:host="videodeeplink"/>
<data android:scheme="videoURL" android:host="videoURL"/>
</intent-filter>
</activity>

To send the data you should add pathPrefix parameter in your data tag like below, so that later we can parse it in the Activity/Fragment where you call it.
<data
android:host="#string/host"
android:pathPrefix="path"
android:scheme="#string/schema" />
Now when you want to parse it you can use pathSegments in Android, like below using the intents to obtain the data inside.
mainIntent = getIntent();
if (mainIntent!=null && mainIntent.getData()!=null
&& (mainIntent.getData().getScheme().equals("http"))){
Uri data = mainIntent.getData();
List<String> pathSegments = data.getPathSegments();
if(pathSegments.size()>0)
String prefix=pathSegments.get(0); // This will give you prefix as path
}

Related

Android Deep Link with a URI that contains no paths but data-only

I want to add support for a deep link of a URI with the following scheme:
xrb:xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp?amount=10000
The amount parameter is optional, the second part is a required address. So essentially the schema is xrb: the first part xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp is any string and amount is an optional parameter.
Looking at the Android documentation here, what I'm not clear about is how to retrieve the data from my activity, particularly for the part that doesn't come in as a parameter (xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp)
The URI is a standard that other applications and services use so I'd rather not change it or do anything proprietary, but is it possible to have a deep link with a URI of this format?
Basically all I'm looking for is when clicking a link containing the URI, the app will open and pre-load some data for them based on the URI.
Thanks!
Accomplished by creating an intent-filter in AndroidManifest
<!-- xrb uri scheme -->
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="xrb" />
</intent-filter>
And retrieving the URI in the activity's onCreate
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
if (data != null) {
Timber.d("inuri %s", data.toString());
} else {
Timber.d("inuri null");
}

Passing extra values to intents via deeplinks in Android

in order for myapp://wallet to point to WalletActivity I wrote the below code, and it worked:
<activity
android:name=".ui.WalletActivity"
android:label="#string/title_activity_wallet"
android:screenOrientation="portrait"
android:theme="#style/MaterialThemeNoActionBar"
android:launchMode="singleTop"
android:exported="true">
<intent-filter android:label="#string/title_activity_link_wallet" >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="myapp"
android:host="wallet" />
</intent-filter>
</activity>
I do a similar thing for UserProfileActivity but it crashes (obviously) inside the function onCreate in the line: String id = intent.getExtras().getString("id"); because I am not sending any value for id to that activity's intent.
So, is there a way for me to pass the id to the intent of the called activity in this case, which I would have done in normal cases using intent.putExtra("id", id); in the calling activity (where intent is of the called activity)
String id = intent.getExtras().getString("id") for the deeplink is wrong.
You can retrieve the Uri associated with the Intent using
Uri uri = getIntent().getData();
you can then using one of the getter available to retrieve your id, if you provide one. E.g. myapp://wallet/test?id=100
String id = data.getQueryParameter("id");
should give you 100

Can't get Uri Data from Deep Link

I want to be redirected from the browser to my App, so I use the following code for my Activity in the Manifest:
<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="fivos"
android:host="book"
/>
</intent-filter>
// Accepts URIs that begin with "fivos://book"
There are also some parameters in the Uri that the browser sends (For example fivos://book?username=george).
In the Activity to which I am redirected, I use the following code to get the Uri but the parameters don't seem to exist in the Uridata object, except the scheme and the host
Uri URIdata = getIntent().getData();
if(URIdata != null){
String scheme = URIdata.getScheme();
String host = URIdata.getHost();
List params = URIdata.getPathSegments();
String username = params.get(0).toString();
}
Am I missing something in my manifest?
I use the following code to get the Uri but the parameters don't seem to exist in the Uridata object, except the sheme and the host
The stuff after the ? in a URL or Uri are the query parameters, which you can retrieve from the Uri via methods like getQuery().

Pass data to an Activity using the "Share via" screen

I'm trying to pass data to my activity, but unfortunately I'm still unsuccessful. What I am trying to accomplish is to select a file in the file browser, share it and pass the data to my activity.
Inside my manifest I added an intent filter:
<activity android:name=".MyActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
Inside my Java file i'm trying to get the data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
// process the data
} else {
// no data received
}
}
When I select a file from My Files and share it, my app is visible in the list, when i click it it launches my activity, but intent.getData(); always returns null.
Am i missing something? Thanks.
you can get the data from the example given below:
Use an with a element. For example, to handle all links to twitter.com, you'd put this inside your in your AndroidManifest.xml:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application.
Of course, if you want to provide tight integration between your website and your app, you can define your own scheme:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, in your web app you can put links like:
<a href="my.special.scheme://other/parameters/here">
And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.
Edit: To answer your question, you can use getIntent().getData() which returns a Uri object. You can then use Uri.* methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:
strong textUri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
You can do the above anywhere in your Activity, but you're probably going to want to do it in onCreate(). You can also use params.size() to get the number of path segments ienter code heren the Uri. Look to javadoc or the android developer website for other Uri methods you can use to extract specific parts.
Launch custom android application from android browser

Taking uri data through intent in android

Hi i have to do connectivity between android app and browser. So while clicking a button on browser it should redirect to android app. in android activity i have written
Uri data = getIntent().getData();
if (data.equals(null)) {
System.out.println("Data is null");
} else {
String scheme = data.getScheme();
System.out.println(scheme);
String host = data.getHost();
int port = data.getPort();
List<String> params = data.getPathSegments();
String first = params.get(0); // "hello"
System.out.println(first);
and in manifest i have already given
<intent-filter>
<data android:scheme="Integration" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in html on button click i have given <FORM METHOD="LINK" ACTION="Integration://1">
it is throwing a indexoutofboundexception.
Please tell me where is the mistake
Updated
*I was unnecessarily using intent in an activity. By removing that n parameter in html5 my app is running successfully now.*
Quoting answer from: How to listen for a custom URI
To register a protocol in your android app, add an extra block to the AndroidManifest.xml
I modified the code a little, but thought I'd quote the source too
<manifest>
<application>
<activity android:name=".activityToCall">
<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="scheme" android:host="path"/>
</intent-filter>
</activity>
</application>
</manifest>
Then when a url matching your schema is opened your app will be called to handle it I assume.
Taking into consideration that scheme and pathcorrespond to this:
scheme://host/path
After that, in the activity you've set in the manifest to handle this stuff:
Uri data = getIntent().getData();
if (!data.equals(null)){
String scheme = data.getScheme();
//Or whatever you needed
}

Categories

Resources