Passing extra values to intents via deeplinks in Android - 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

Related

How to give Intent Data in Deep Link

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
}

How to receive a URL from my android app after being forwarded from browser.

When a user visits the browser page for my app I am forwarding them to a url scheme like so:
intent://share?id=123#Intent;package=com.aerstone.mobile;scheme=myapp;launchFlags=268435456;end;
I've tested this and this works fine. Now, when the user visits my app's home page from the browser and have the app installed, the app automatically opens up.
Relevant part from my manifest is:
<activity android:configChanges="keyboardHidden|orientation" android:label="#string/app_name"
android:name=".ui.activity.MainActivity"
android:launchMode="singleTask" >
<!-- add the above launchMode attribute -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- add the below additional intent-filter -->
<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="myapp"/>
</intent-filter>
</activity>
Question
After the app opens up, how can I fetch the URL? I want to fetch the id=123 part of the URL. How can I do this?
Call getIntent().getData() in onCreate(), or getData() on the passed-in Intent to onNewIntent(), depending on whether your activity was already created or not. That will return the Uri representing your URL.
In onCreate()
Uri uri = getIntent().getData(); //Returns the URI that was used.
String id = uri.getQueryParameter("id"); //Gives any parameter passed in the URI.

Issue with Android intent filter

I have created an Intent filter to open my application when clicking on an specified link such as "http://www.myapp.com/1234". When I click on the link I am asked to open the app, and OK, everything works well. Then, I quit from the app by clicking repeatedly in the back button until all activities are closed. But:
After launching the app again from the launcher, it receives the same Intent data than
before when I clicked the link. How do avoid it?
When I open my app from a link, ie. from WhatsApp, as I can see in the app manager, WhatsApp is the application which is opened! why?
Here is my current manifest code:
<activity
android:name="com.x.y.SplashActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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:host="#string/app_host"
android:pathPrefix="/events/"
android:scheme="http" />
</intent-filter>
</activity>
and in my onCreate():
// Retrieve the Intent
Intent intentUri = getIntent();
// Check URI
Uri uri = intentUri.getData();
if (uri != null) {
// Try to extract the data
// For now, data could be a public event id, so parse it
List<String> pathSegments = uri.getPathSegments();
// Check validity
if (pathSegments.size() > 2) {
// Finally, get the public event id
String publicEventId = pathSegments.get(2);
// Set poll as responded
user.setPollEventId(publicEventId);
}
// Remove the intent to prevent further calls
setIntent(null);
}
// Open the main activity normally
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

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
}

Matching a URL to an Intent not working

I'm writing an Android app that accompanies a website and I wanted to make the app intercept certain URLs. I found multiple examples through Google and on SO but none of them work. I create my Intent:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://xgn.nl/article/" + String.valueOf(id)));
and then launch it from another Activity (where i is the intent from above)
startActivity(i);
The intent filter for the activity that should receive this is:
<activity android:name=".ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" android:mimeType="text/*" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
I also tried it without the mimeType in the filter. The intent resolver sees the filter but complains that the data does not match with the data in the intent and launches the browser instead.
What am I doing wrong?
I actually solved it 5s after I posted it, the problem was that you need to specify the fully qualified class name for this to work :)
<activity android:name="nl.xgn.ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
EDIT: To be more precise, the error I was seeing in logcat was wrong. The data type did match, but it chose the more visible browser class over my lazily defined activity. Specifying the full class name and removing the mimeType made this work.

Categories

Resources